5
votes

We know that if we set a shortcut (for example, Ctrl + F2) to a TMenuItem, the menu item will be executed automatically once the shortcut specified is pressed, and the shortcut description will be also shown when the menu is displayed.

But is there a way to have the shortcut descriptions visible on the menu items but make the menu don't respond to the shortcuts automatically?

You might ask me why I want this, here is the situation:

In a multiple document (like firefox's multiple tabs) program, there are multiple instances of TPopupMenu thus multiple TMenuItem objects have the same shortcuts, but I only want the menuitems in the active document window respond to the shortcuts.


Edit 1: Sorry, I wanted to simplify my question and I described it wrongly - actually, I use TActionList and link the actions to menu items.


Edit 2: Just found: I think I can use TApplicationEvents.OnShortCut Event to intercept the shortcuts before they are being dispatched to the menus/actions... I'll try and will update my questions when I get a result.

1
TLama, thanks for the help. I've updated(corrected) my questions. By 'multiple instances of TPopupMenu (should be TActionList after I corrected my question)', I mean I have created multiple TFrame objects which of each contains a TActionList.Edwin Yip
But even if it's about action list, still only one focused frame's menu will respond to the shortcut. I don't get why to intercept the shortcuts if they are described in such menu item captions. It will be confusing for users, they will be able to popup the menu, can see the shortcut, but nothing happens when they use the keystroke.TLama
@TLama, I just verified again, with D2010, it's not the focused frame's menu will respond to the shortcut, but the unexpected frame's action will be executed and the rest will be skipped. This is actually will confuse the users. What I want to do is to avoid such confusions by centralizing the processing of the shortcuts so that only the active frame will handle the shortcuts, while still let the users see the shortcuts on the menu items by using the TAction.Shortcut property.Edwin Yip
You should fundamentally change the design so that each frame shares a single ActionList, a single Popupmenu, etc. When a frame is selected you would define a pointer (something like PActiveDocument: ^TmyFrame). Then in the action.execute implementation you would only use your PActiveDocument. In addition you app would also use less memory.az01
@az01, thanks, that's an alternative approach, however, having the actions on frame gives me convenience (accessing the frame's internal data).Edwin Yip

1 Answers

6
votes

Use the tab (#9) character to indicate the shorcut part of the text in standard menus. You can set the Caption property of the menu item or the action component that the menu item is bound to by either editing the 'dfm' or at run-time to include the tab character:

procedure TForm1.FormCreate(Sender: TObject);
begin
  Action1.Caption :=
      Action1.Caption + #9 + ShortCutToText(ShortCut(VK_F2, [ssCtrl]));

Unless you also assign to the ShortCut property itself of the menu item or the action, the click/execute event will not be fired.