以编程方式为 DELETE 键创建快捷方式字符串?
Programmatically create a Shortcut STRING for the DELETE key?
在 Delphi 10.4.2 Win32 VCL 应用程序 Windows 10 x64 中,我使用此代码以编程方式为弹出菜单项上的 DELETE 键创建快捷方式字符串:
mGalleryDeleteSelected.Caption := mGalleryDeleteSelected.Caption + #9 + MyShortcutToString(VK_DELETE, []) + ' ';
这是源代码:
function MyGetKeyName(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 MyModifierVirtualKey(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 MyShortcutToString(AKey: Integer; AShiftState: TShiftState = []): string;
begin
Result := '';
for var Modifier in AShiftState do
begin
var ModifierKey := MyModifierVirtualKey(Ord(Modifier));
if ModifierKey <> 0 then
Result := Result + IfThen(not Result.IsEmpty, '+') + MyGetKeyName(ModifierKey);
end;
Result := Result + IfThen(not Result.IsEmpty, '+') + MyGetKeyName(AKey);
end;
但是,我没有获取普通 DELETE 键的快捷字符串,而是获取数字小键盘上(辅助?)Comma/Delete 键的快捷字符串:
因为我会德语 Windows,这里是翻译:
KOMMA = 逗号
ZEHNERTASTATUR = 数字小键盘
德语键盘上的默认 Delete 键具有标签“entf”(“Entfernen”的缩写)
如果数字小键盘设置为适用于导航命令,则按下数字小键盘上的逗号键确实可以用作删除命令。但是,我需要获取 NORMAL DELETE 键的字符串。我该怎么做?
来自 GetKeyNameText
的文档:
24 Extended-key flag. Distinguishes some keys on an enhanced keyboard.
这表示它使用 LPARAM 样式参数中的第 24 位作为扩展密钥标志。
然后,在About Keyboard Input:
Extended-Key Flag
The extended-key flag indicates whether the keystroke message originated from one of the additional keys on the enhanced keyboard. The extended keys consist of the ALT and CTRL keys on the right-hand side of the keyboard; the INS, DEL, HOME, END, PAGE UP, PAGE DOWN, and arrow keys in the clusters to the left of the numeric keypad; the NUM LOCK key; the BREAK (CTRL+PAUSE) key; the PRINT SCRN key; and the divide (/) and ENTER keys in the numeric keypad. The extended-key flag is set if the key is an extended key.
(正文强调我的)。
因此,我得出结论,您可以使用
function MyGetKeyName(AKey: Integer): string;
var
name: array[0..128] of Char;
begin
FillChar(name, SizeOf(name), 0); // ADD THIS!
GetKeyNameText(MapVirtualKey(AKey, 0) shl 16 or 1 shl 24, @name[0], Length(name));
Result := name;
end;
获取键盘字母和数字部分之间 Del
键的名称。
当然,您可能不想为其他键设置此位,因此您需要重构代码。不幸的是,VK_DELETE
似乎可以映射到两个物理键。
这是一种方法:
function MyGetKeyName(AKey: Integer; AExtended: Boolean = False): string;
var
name: array[0..128] of Char;
begin
FillChar(name, SizeOf(name), 0);
if // ADD THIS!
GetKeyNameText(MapVirtualKey(AKey, 0) shl 16 or Cardinal(Ord(AExtended)) shl 24,
@name[0], Length(name)) <> 0
then
Result := name
else
Result := '';
end;
并将此参数也添加到您的 MyShortcutToString
(只需要将其传递给 MyGetKeyName
):
function MyShortcutToString(AKey: Integer; AShiftState: TShiftState = [];
AExtended: Boolean = False): string;
begin
Result := '';
for var Modifier in AShiftState do
begin
var ModifierKey := MyModifierVirtualKey(Ord(Modifier));
if ModifierKey <> 0 then
Result := Result + IfThen(not Result.IsEmpty, '+') + MyGetKeyName(ModifierKey);
end;
Result := Result + IfThen(not Result.IsEmpty, '+') + MyGetKeyName(AKey, AExtended);
end;
在 Delphi 10.4.2 Win32 VCL 应用程序 Windows 10 x64 中,我使用此代码以编程方式为弹出菜单项上的 DELETE 键创建快捷方式字符串:
mGalleryDeleteSelected.Caption := mGalleryDeleteSelected.Caption + #9 + MyShortcutToString(VK_DELETE, []) + ' ';
这是源代码:
function MyGetKeyName(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 MyModifierVirtualKey(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 MyShortcutToString(AKey: Integer; AShiftState: TShiftState = []): string;
begin
Result := '';
for var Modifier in AShiftState do
begin
var ModifierKey := MyModifierVirtualKey(Ord(Modifier));
if ModifierKey <> 0 then
Result := Result + IfThen(not Result.IsEmpty, '+') + MyGetKeyName(ModifierKey);
end;
Result := Result + IfThen(not Result.IsEmpty, '+') + MyGetKeyName(AKey);
end;
但是,我没有获取普通 DELETE 键的快捷字符串,而是获取数字小键盘上(辅助?)Comma/Delete 键的快捷字符串:
因为我会德语 Windows,这里是翻译:
KOMMA = 逗号
ZEHNERTASTATUR = 数字小键盘
德语键盘上的默认 Delete 键具有标签“entf”(“Entfernen”的缩写)
如果数字小键盘设置为适用于导航命令,则按下数字小键盘上的逗号键确实可以用作删除命令。但是,我需要获取 NORMAL DELETE 键的字符串。我该怎么做?
来自 GetKeyNameText
的文档:
24 Extended-key flag. Distinguishes some keys on an enhanced keyboard.
这表示它使用 LPARAM 样式参数中的第 24 位作为扩展密钥标志。
然后,在About Keyboard Input:
Extended-Key Flag
The extended-key flag indicates whether the keystroke message originated from one of the additional keys on the enhanced keyboard. The extended keys consist of the ALT and CTRL keys on the right-hand side of the keyboard; the INS, DEL, HOME, END, PAGE UP, PAGE DOWN, and arrow keys in the clusters to the left of the numeric keypad; the NUM LOCK key; the BREAK (CTRL+PAUSE) key; the PRINT SCRN key; and the divide (/) and ENTER keys in the numeric keypad. The extended-key flag is set if the key is an extended key.
(正文强调我的)。
因此,我得出结论,您可以使用
function MyGetKeyName(AKey: Integer): string;
var
name: array[0..128] of Char;
begin
FillChar(name, SizeOf(name), 0); // ADD THIS!
GetKeyNameText(MapVirtualKey(AKey, 0) shl 16 or 1 shl 24, @name[0], Length(name));
Result := name;
end;
获取键盘字母和数字部分之间 Del
键的名称。
当然,您可能不想为其他键设置此位,因此您需要重构代码。不幸的是,VK_DELETE
似乎可以映射到两个物理键。
这是一种方法:
function MyGetKeyName(AKey: Integer; AExtended: Boolean = False): string;
var
name: array[0..128] of Char;
begin
FillChar(name, SizeOf(name), 0);
if // ADD THIS!
GetKeyNameText(MapVirtualKey(AKey, 0) shl 16 or Cardinal(Ord(AExtended)) shl 24,
@name[0], Length(name)) <> 0
then
Result := name
else
Result := '';
end;
并将此参数也添加到您的 MyShortcutToString
(只需要将其传递给 MyGetKeyName
):
function MyShortcutToString(AKey: Integer; AShiftState: TShiftState = [];
AExtended: Boolean = False): string;
begin
Result := '';
for var Modifier in AShiftState do
begin
var ModifierKey := MyModifierVirtualKey(Ord(Modifier));
if ModifierKey <> 0 then
Result := Result + IfThen(not Result.IsEmpty, '+') + MyGetKeyName(ModifierKey);
end;
Result := Result + IfThen(not Result.IsEmpty, '+') + MyGetKeyName(AKey, AExtended);
end;