Processing Keyboard Messages

The window procedure of the window that has the keyboard focus receives keystroke messages when the user types on the keyboard. An application that responds to keyboard input typically processes WM_KEYDOWN messages only.

In general, you should use the TranslateMessage function in your message loop to translate every message, not just keystroke messages. Although TranslateMessage has no effect on other types of messages, using it ensures that keyboard input is translated correctly.

When a window procedure receives the WM_CHAR message, it should examine the character code that accompanies the message to determine how to process the character.

If a window procedure processes system keyboard messages, it should pass the message to the DefWindowProc function. Otherwise, all system operations involving the ALT key will be disabled whenever that window has the keyboard focus. Windows CE uses the WM_SYSCHAR message to implement menu mnemonics.

The lParam parameter of a keystroke message contains the following additional information about the keystroke that generated the message.

Information type Explanation
Repeat count Specifies the number of times the keystroke was repeated as a result of the user holding down the key.
Scan code Gives the hardware-dependent scan code of the key.
Context code Has a value of one if the ALT key was pressed and of zero if it was released.
Previous key state Has a value of one if the pressed key was previously down and zero if it was previously up. It has a value of one for WM_KEYDOWN and WM_SYSKEYDOWN keystroke messages generated by the automatic repeat feature.
Transition state Has a value of one if the key was released or of zero if it was pressed.