Why You Should Use Keyboard Accelerators

You may ask: Why should I use keyboard accelerators? Why can't I simply trap WM- KEYDOWN or WM_CHAR messages and duplicate the menu functions myself? What's the advantage? For a single-window application, you can certainly trap keyboard messages, but you get certain advantages from using keyboard accelerators: You don't need to duplicate the menu and keyboard accelerator logic. If the keyboard accelerator duplicates a menu function, Windows flashes the top-level item on the menu when a keyboard accelerator is used, thus providing some visual feedback to the user.

For applications with multiple windows and multiple window procedures, keyboard accelerators become very important. As we've seen, Windows sends keyboard messages to the window procedure for the window that currently has the input focus. For keyboard accelerators, however, Windows sends the WM_COMMAND message to the window procedure whose handle is specified in the Windows function TranslateAccelerator. Generally, this will be your main window, the same window that has the menu, which means that the logic for acting upon keyboard accelerators does not have to be duplicated in every window procedure.

This advantage becomes particularly important if you use modeless dialog boxes (discussed in Chapter 10) or child windows on your main window's client area. If a particular keyboard accelerator is defined to move among windows, then only one window procedure has to include this logic. The child windows do not receive WM_COMMAND messages from the keyboard accelerators.