如何获取包含 SHIFTSTATE 的快捷键的字符串表示形式?
How to get the string representation of a ShortCut Key including the SHIFTSTATE?
在 Delphi 10.4.2 Win32 VCL 应用程序中,基于问题+解决方案 ,它提供了一种获取快捷键的字符串表示形式的方法(但大概不可能也为快捷键传递一个 SHIFTSTATE)我写了这段代码:
function MyGetSpecialShortcutName(ShortCut: TShortCut): string;
// gets shortcut name for e.g. VK_NUMPAD0 where TMenuItem.Shortcut gets the wrong shortcut name
var
ScanCode: Integer;
KeyName: array[0..255] of Char;
begin
Result := '';
FillChar(KeyName, SizeOf(KeyName), 0);
ScanCode := Winapi.Windows.MapVirtualKey(LoByte(Word(ShortCut)), 0) shl 16;
if ScanCode <> 0 then
begin
if Winapi.Windows.GetKeyNameText(ScanCode, KeyName, Length(KeyName)) <> 0 then
Result := KeyName;
end;
end;
function GetSpecialShortcutNameWithShiftState(const AScanCode: Word; const AShiftState: System.Classes.TShiftState = []): string;
begin
Result := MyGetSpecialShortcutName(Vcl.Menus.ShortCut(AScanCode, AShiftState));
end;
用法:
Result := GetSpecialShortcutNameWithShiftState(VK_A, [ssCTRL]);
但是,结果为“A”,而预期结果应为“CTRL+A”。
如何获取包含 SHIFTSTATE 的快捷键的字符串表示形式?
OP 想要键名完全本地化,但为了完整起见,我首先说明 VCL 已经有一个函数来获取部分未本地化的字符串,即 ShortCutToText
在Menus
单元:
ShortCutToText(ShortCut(Ord('A'), [ssShift, ssAlt]))
这 returns Shift+Alt+A
在所有系统上。
现在,使用Q中已经提到的Win32函数GetKeyNameText
,很容易获得完全本地化的快捷方式字符串:
function GetKeyName(AKey: Integer): string;
var
name: array[0..128] of Char;
begin
FillChar(name, SizeOf(name), 0);
GetKeyNameText(MapVirtualKey(AKey, 0) shl 16, @name[0], Length(name));
Result := name;
end;
function ModifierVirtualKey(AModifier: Integer): Integer;
begin
case AModifier of
Ord(ssShift):
Result := VK_SHIFT;
Ord(ssCtrl):
Result := VK_CONTROL;
Ord(ssAlt):
Result := VK_MENU;
else
Result := 0;
end;
end;
function ShortcutToString(AKey: Integer; AShiftState: TShiftState = []): string;
begin
Result := '';
for var Modifier in AShiftState do
begin
var ModifierKey := ModifierVirtualKey(Ord(Modifier));
if ModifierKey <> 0 then
Result := Result + IfThen(not Result.IsEmpty, '+') + GetKeyName(ModifierKey);
end;
Result := Result + IfThen(not Result.IsEmpty, '+') + GetKeyName(AKey);
end;
(这里我使用了 StrUtils
中的 IfThen
overload。)
现在,
ShortcutToString(Ord('A'), [ssShift, ssAlt])
returns SKIFT+ALT+A
在我的瑞典语系统上。 SKIFT
是,您可能已经猜到了,SHIFT
键的瑞典语名称。
在 Delphi 10.4.2 Win32 VCL 应用程序中,基于问题+解决方案
function MyGetSpecialShortcutName(ShortCut: TShortCut): string;
// gets shortcut name for e.g. VK_NUMPAD0 where TMenuItem.Shortcut gets the wrong shortcut name
var
ScanCode: Integer;
KeyName: array[0..255] of Char;
begin
Result := '';
FillChar(KeyName, SizeOf(KeyName), 0);
ScanCode := Winapi.Windows.MapVirtualKey(LoByte(Word(ShortCut)), 0) shl 16;
if ScanCode <> 0 then
begin
if Winapi.Windows.GetKeyNameText(ScanCode, KeyName, Length(KeyName)) <> 0 then
Result := KeyName;
end;
end;
function GetSpecialShortcutNameWithShiftState(const AScanCode: Word; const AShiftState: System.Classes.TShiftState = []): string;
begin
Result := MyGetSpecialShortcutName(Vcl.Menus.ShortCut(AScanCode, AShiftState));
end;
用法:
Result := GetSpecialShortcutNameWithShiftState(VK_A, [ssCTRL]);
但是,结果为“A”,而预期结果应为“CTRL+A”。
如何获取包含 SHIFTSTATE 的快捷键的字符串表示形式?
OP 想要键名完全本地化,但为了完整起见,我首先说明 VCL 已经有一个函数来获取部分未本地化的字符串,即 ShortCutToText
在Menus
单元:
ShortCutToText(ShortCut(Ord('A'), [ssShift, ssAlt]))
这 returns Shift+Alt+A
在所有系统上。
现在,使用Q中已经提到的Win32函数GetKeyNameText
,很容易获得完全本地化的快捷方式字符串:
function GetKeyName(AKey: Integer): string;
var
name: array[0..128] of Char;
begin
FillChar(name, SizeOf(name), 0);
GetKeyNameText(MapVirtualKey(AKey, 0) shl 16, @name[0], Length(name));
Result := name;
end;
function ModifierVirtualKey(AModifier: Integer): Integer;
begin
case AModifier of
Ord(ssShift):
Result := VK_SHIFT;
Ord(ssCtrl):
Result := VK_CONTROL;
Ord(ssAlt):
Result := VK_MENU;
else
Result := 0;
end;
end;
function ShortcutToString(AKey: Integer; AShiftState: TShiftState = []): string;
begin
Result := '';
for var Modifier in AShiftState do
begin
var ModifierKey := ModifierVirtualKey(Ord(Modifier));
if ModifierKey <> 0 then
Result := Result + IfThen(not Result.IsEmpty, '+') + GetKeyName(ModifierKey);
end;
Result := Result + IfThen(not Result.IsEmpty, '+') + GetKeyName(AKey);
end;
(这里我使用了 StrUtils
中的 IfThen
overload。)
现在,
ShortcutToString(Ord('A'), [ssShift, ssAlt])
returns SKIFT+ALT+A
在我的瑞典语系统上。 SKIFT
是,您可能已经猜到了,SHIFT
键的瑞典语名称。