Keyboard Shortcuts must be attached to Menu Items
|Product/Release:||LANSA for Windows|
|Abstract:||Keyboard Shortcuts must be attached to Menu Items|
|Submitted By:||LANSA Technical Support|
A problem occurs when trying to create a popup menu and include on this popup a menu 1 item. This item has an event routine to invoke on Click and also a Keyboard Shortcut. The action performs OK when the Popup menu item is clicked but nothing happens when the Keyboard Shortcut is used. There is, however, a good reason as to why the Keyboard Shortcut does not work in this situation.
Essentially, Shortcuts (eg. Ctrl+E, Alt+B, Ctrl+Shift+F, etc. etc.) on a popup menu will only perform the action if the popup menu item is also included as a submenu item on a menu bar.
For example, on a new empty form a popup menu is created and an entry called Exit is included. A shortcut of Ctrl+E is selected from the popup menu's properties. A Click event routine for this popup menu item is also created which performs the following action INVOKE <Form Name>.CloseForm
This form is compiled and executed . At execution time the click closes the form OK but using the shortcut Ctrl+E does not work. When Ctrl+E is used, nothing happens. This is actually correct.
Next, a menu bar and a menu item are dragged onto the form. The menu item is given a Caption of "Test". a submenu onto this menu item. The submenu has one entry called Exit with a shortcut of Ctrl+E. Again, a Click event routine for this popup menu item is created which performs the following action INVOKE <Form Name>.CloseForm. At execution time the form is started and when Ctrl+E is used, the form is shut down. This is also correct. The reason is that for a shortcut to work it MUST be a submenu item on a menu bar. If the shortcut is part of a popup menu only, (but not a menu item on a menu bar) then nothing will happen if Ctrl+E is used at execution time.
This also can be tested in Microsoft Word. In Word, in a blank document, the right mouse button can be used to get a popup menu. In this popup menu it is possible to Paste or to use the shortcut Ctrl+V. The only reason that Ctrl+V works is because the Paste option is also a menu item in a submenu from the menu bar ie. Edit -> Paste. If the paste option was not an item of the Edit menu then the Ctrl+V would not work.
In conclusion, if a shortcut is needed to perform an action, then it is necessary to put a menu bar on the form with a menu item and include the shortcut as a property for the menu item. Then the shortcut can be used.
If only a popup menu is needed on the form, without a menu bar and submenus, then a click event routine for the popup menu item can be invoked to initiate the action. Another option is to place an ampersand before one of the letters in the caption to invoke the action in the click event routine when Alt+<the letter> is selected eg. &Exit will invoke the click event by typing Alt+E.