36.2.1 Processing Keystrokes Messages

The window procedure of the window that has the keyboard focus receives keystroke messages when the user types at the keyboard. The keystroke messages consist of WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, and WM_SYSKEYUP. A typical window procedure ignores all keystroke messages except WM_KEYDOWN. Windows posts the WM_KEYDOWN message when the user presses a key.

When the window procedure receives the WM_KEYDOWN message, it should examine the virtual-key code that accompanies the message to determine how to process the keystroke. The virtual-key code is in the message's wParam parameter. Typically, an application only processes keystrokes generated by non-character keys, including the function keys, the cursor movement keys, and the special-purpose keys such as INSERT, DELETE, HOME and END.

The following code fragment shows the window-procedure framework that a typical application uses to receive and process keystroke messages:

case WM_KEYDOWN:

switch (wParam) {

case VK_LEFT:

.

. /* Process the left arrow key. */

.

break;

case VK_RIGHT:

.

. /* Process the right arrow key. */

.

break;

case VK_UP:

.

. /* Process the up arrow key. */

.

break;

case VK_DOWN:

.

. /* Process the down arrow key. */

.

break;

case VK_HOME:

.

. /* Process the HOME key. */

.

break;

case VK_END:

.

. /* Process the END key. */

.

break;

case VK_INSERT:

.

. /* Process the INSERT key. */

.

break;

case VK_DELETE:

.

. /* Process the DELETE key. */

.

break;

case VK_F2:

.

. /* Process the F2 key. */

.

break;

.

. /* Process other non-character keystrokes. */

.

default:

break;

}