48.1.4 Accelerators and Menus

The result of pressing an accelerator is the same as choosing a menu item—both actions cause a WM_COMMAND (or WM_SYSCOMMAND) message to be sent to the corresponding window procedure. The WM_COMMAND message includes an identifier that the window procedure can examine to determine the source of the message. If an accelerator generated the WM_COMMAND message, the identifier is that of the accelerator. Likewise, if a menu item generated the WM_COMMAND message, the identifier is that of the menu item. Because an accelerator is typically provided as a shortcut to choosing a command provided by a menu item, an application usually assigns the same identifier to the accelerator as it assigns to the corresponding menu item.

Typically, an application processes a WM_COMMAND message generated by an accelerator in exactly the same way it processes a WM_COMMAND message sent from the corresponding menu item. However, the WM_COMMAND message contains a flag that specifies whether the message originated with an accelerator or a menu item, in case you need to process accelerators differently from their corresponding menu items. The WM_SYSCOMMAND message does not contain this flag.

An accelerator's identifier determines whether the accelerator generates a WM_COMMAND or WM_SYSCOMMAND message. If an accelerator's command identifier has the same value as a menu item in the System menu, the accelerator generates a WM_SYSCOMMAND message. Otherwise, the accelerator generates a WM_COMMAND message.

If an accelerator has the same identifier as a menu item and the menu item is grayed or disabled, the accelerator is disabled. That is, using the accelerator does not generate a WM_COMMAND or WM_SYSCOMMAND message. Also, an accelerator doesn't generate a command message if the corresponding window is minimized.

When the user presses an accelerator that corresponds to a menu item, your window procedure receives the WM_INITMENU and WM_INITMENUPOPUP messages just as if the user selected the menu item. For information on how to process these messages, see Chapter 47, “Menus.”

When you define an accelerator that corresponds to an item in a menu, you should include the accelerator in the text of the menu item. For example, the Windows Write application includes accelerators in the text of the first four items in its Edit menu: