【问题标题】:FMX: How to create a menu item shortcut for a single key?FMX:如何为单个键创建菜单项快捷方式?
【发布时间】:2021-12-18 14:22:46
【问题描述】:

我需要它来为 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 位构建。

【问题讨论】:

    标签: firemonkey


    【解决方案1】:

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

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      相关资源
      最近更新 更多