如何在Firemonkey中拦截菜单快捷方式事件

问题描述:

在Firemonkey表单上,添加一个主菜单和一个子菜单项.将子菜单的快捷方式属性设置为Ctrl-A.

On a Firemonkey Form add a main menu and a single submenu item. Set the shortcut property for the submenu to Ctrl-A.

在进入菜单之前,是否有拦截Ctrl-A的机会?似乎OnKeyDown表单看不到它.

Is there anyway to intercept the Ctrl-A before it gets to the menu? It seems that the form OnKeyDown doesn't see it.

该表单检查是否存在要处理该密钥的子组件.如果处理了密钥,那么故事就结束了.

The form checks if there are child components that want to handle the key. If the key is handled then that is the end of the story.

这是一种快速而肮脏的方法,可以防止TMenuItem使用插入器来处理密钥.

Here's a quick&dirty way that prevents the TMenuItem from handling the key, using an interposer.

type
  TMenuItem = class(FMX.Menus.TMenuItem)
  protected
    procedure DialogKey(var Key: Word; Shift: TShiftState); override;
  end;


procedure TMenuItem.DialogKey(var Key: Word; Shift: TShiftState);
begin
  if (ssCtrl in Shift) and (Key = 65){A} then exit;       
  inherited;
end;

如果要使用动作,则必须以相同的方式覆盖TActionListDialogKey功能.

If you are using actions then you have to override the DialogKey function of the TActionList in the same way.