FMX:如何为单个键创建菜单项快捷方式



我需要它来为TMainMenu和TMenuBar中的单个键创建快捷方式,例如"I"。对于TMenuItem的快捷方式属性,在属性编辑器中无法选择单个键的选项。我还尝试过在运行时使用以下任一方法设置菜单项的快捷方式。

MenuItem.ShortCut := vkI;
MenuItem.ShortCut := TextToShortcut('I');

快捷键字符"I"确实出现在菜单中,但它不起作用。我按了"I"键,什么也没发生。然而,如果我使用以下方法设置快捷方式,那么它确实可以正常工作,所以我认为问题只是它不允许使用单键快捷方式。

MenuItem.ShortCut := TextToShortcut('Ctrl+I');

我还尝试过将菜单项链接到TActionList中的某个操作,并以相同的方式设置该操作的快捷方式,但结果是一样的。

我找到的解决方案是处理FormKeyDown事件中的按键来触发操作,但这似乎没有必要。为什么它不能按预期工作?

我使用的是Delphi10.4,并为Windows32位构建。

因为您的快捷方式不包含对话键。

procedure TCommonCustomForm.IsDialogKey(const Key: Word; const KeyChar: WideChar; const Shift: TShiftState;
var IsDialog: boolean);
begin
IsDialog := (KeyChar < ' ') or ((Shift * [ssAlt, ssCtrl, ssCommand]) <> []);
end;

TCommonCustomForm.KeyDown中,代码开始检查您的快捷方式是否包含对话框键,如果是,它将检查您的菜单并执行操作:

// 3. perform key in other Menus
for I := ChildrenCount - 1 downto 0 do
if Children[i] <> FocusPopup then
begin
if Children[I] is TMainMenu then
TMainMenu(Children[I]).DialogKey(Key, Shift)
else if Children[I] is TPopupMenu then
TPopupMenu(Children[I]).DialogKey(Key, Shift);
if Key = 0 then
Exit;
end;

您可以覆盖此方法,并为按下的每个键返回true

最新更新