【发布时间】:2023-03-25 05:29:01
【问题描述】:
在 Delphi 10.4.2 Win32 VCL 应用程序中,并基于问题 + 解决方案 here,它提供了一种获取快捷键的字符串表示的方法(但大概不可能也为快捷键传递 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 的快捷键的字符串表示形式?
【问题讨论】:
-
您需要本地化修饰符吗? (例如,英语中的“Ctrl+A”和德语中的“Strg+A”。)
-
是的!谢谢你的提问!
标签: delphi keyboard-shortcuts delphi-10.4-sydney