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

FMX: How to create a menu item shortcut for a single key?

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

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

快捷字符 'I' 确实出现在菜单中,但是它不起作用。我按下 'I' 键,但没有任何反应。但是,如果我使用以下设置快捷方式,那么它确实可以正常工作,所以我认为问题只是它不允许单键快捷方式。

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

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

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

我正在使用 Delphi 10.4 并构建 Windows 32 位。

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

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;

您可以覆盖此方法,并且 return true 您按下的每个键