Windows provides a standard keyboard interface to menus. You can enhance this interface by including mnemonics and keyboard accelerators for your menu items. The following three sections describe the standard keyboard interface, mnemonics, and accelerators.
Windows is designed to work with or without a mouse or other pointing device, providing a standard keyboard interface for menus. You don't need to provide code to support this keyboard interface. Your application receives a command message when a menu item is chosen by using the keyboard just as if it had been chosen by using a mouse. The standard keyboard interface processes the following keystrokes:
Keystroke | Action |
ALT | Toggles into and out of menu-bar mode. |
ALT+SPACEBAR | Shows the System menu. |
ESC | Exits menu mode. |
RIGHT | Cycles to the next top-level menu item. Cycles between the application and System menus. If the selected item is in a pop-up menu, the next column in the pop-up menu is selected or the next top-level menu item is selected; this keystroke may also send a WM_INITMENUPOPUP message. |
LEFT | Works like the RIGHT key, except in the opposite direction. In pop-up menus, this keystroke backs up one column, except when the currently selected item is in the far-left column, in which case the previous pop-up menu is selected. |
UP or DOWN | Activates a pop-up menu when pressed in a top-level menu. When pressed in a pop-up menu, this keystroke selects the next or previous item. |
ENTER | Activates a pop-up menu and highlights the first item if an item has a pop-up menu associated with it; otherwise, this keystroke chooses the item as if the user released the mouse button while the item was selected. |
Alphabetic character | Selects the first menu item with the specified character as its mnemonic. If the selected item invokes a pop-up menu, the menu is displayed and the first item is highlighted; otherwise, the item is chosen. |
You can enhance the standard keyboard interface to menus by adding mnemonics to menu items. A mnemonic is a underlined letter in the text of a menu item. When a menu is active, the user can select a menu item by pressing the key that corresponds to the item's underlined letter. (The menu bar is active when the user presses and releases the ALT key and the first item in the menu bar is highlighted. A pop-up menu is active when it is displayed.)
You indicate a mnemonic keystroke for a menu item by preceding a character in the item's string with an ampersand, as in “&File,” which causes Windows to underline the character “F”.
In addition to having a mnemonic, a menu item can have a keyboard accelerator associated with it. A keyboard accelerator is different from a mnemonic in that the menu need not be active for the accelerator key to work. Also, a mnemonic is always associated with a menu item—a keyboard accelerator is usually associated with a menu item, but does not need to be.
If you associate a keyboard accelerator with a menu item, you should include text in the menu item that identifies the accelerator. This text should be displayed at the right end of the menu item. You can do this by placing a tab character (\t) in the menu text before the characters that should be displayed on the right. For example, if the Close item had the F3 function key as its keyboard accelerator, the text for the item would be “&Close\tF3”. For more information on accelerators, see Chapter 48, “Keyboard Accelerators.”