Ignoring the Keyboard

Although the keyboard is the primary source of user input to Windows programs, your program does not need to act on every keyboard message it receives. Windows handles many keyboard functions itself. For instance, you can ignore keystrokes that pertain to system functions. These keystrokes generally involve the Alt key.

A program need not monitor these keystrokes itself because Windows notifies a program of the effect of the keystrokes. (A program can monitor the keystrokes if it wants to, however.) For instance, if the Windows user selects a menu item with the keyboard, Windows sends the program a message that the menu item has been selected, regardless of whether it was selected by using the mouse or by using the keyboard. (Menus are covered in Chapter 9.)

Some Windows programs use ”keyboard accelerators“ to invoke common menu items. The accelerators generally involve the function keys, special noncharacter keys such as Insert or Delete, or a letter in combination with the Ctrl key. These keyboard accelerators are defined in a program's resource script. (Chapter 9 shows how Windows translates the accelerators into menu command messages. You don't have to do the translation yourself.)

Dialog boxes (covered in Chapter 10) also have a keyboard interface, but programs usually do not need to monitor the keyboard when a dialog box is active. The keyboard interface is handled by Windows, and Windows sends messages to your program about the effects of the keystrokes. Dialog boxes can contain ”edit“ controls for text input. These are generally small boxes in which the user types a character string. Windows handles all the edit control logic and gives your program the final contents of the edit control when the user is done.

Even within your main window you can define child windows that function as edit controls. An extreme example of this is the Windows NOTEPAD program, which is little more than a large multiline edit control. NOTEPAD does little keyboard processing on its own and relies on Windows to handle all the dirty work. (Chapter 6 discusses how this works.)