The lParam Variable

For all four keystroke messages, the 32-bit lParam variable passed to the window procedure is divided into six fields: Repeat Count, OEM Scan Code, Extended Key Flag, Context Code, Previous Key State, and Transition State. (See Figure 3-1.)

Repeat Count

The Repeat Count is the number of keystrokes represented by the message. In most cases the Repeat Count is set to 1. However, if a key is held down and your window procedure is not fast enough to process key-down messages at the typematic rate (approximately a 10-character-per-second default), Windows combines several WM_KEYDOWN or WM_SYSKEYDOWN messages into a single message and increases Repeat Count accordingly. The Repeat Count is always 1 for a WM_KEYUP or WM_SYSKEYUP message.

Because a Repeat Count greater than 1 indicates that typematic keystrokes are occurring faster than your program can process them, you may want to ignore the Repeat Count when processing the keyboard messages. Almost everyone has had the experience of ”overscrolling“ a word-processing document or spreadsheet because extra keystrokes have stacked up in the keyboard buffer. Ignoring the Repeat Count in your program will significantly reduce the possibilities for overscrolling. However, in other cases you will want to use the Repeat Count. You should probably try your programs both ways and see which approach feels the most natural.

OEM Scan Code

The OEM Scan Code is the keyboard scan code generated by the hardware of the computer. For the IBM PC, this scan code is the same as the value passed back to a program in register AH during a BIOS Interrupt 16H call. Windows applications generally ignore the OEM Scan Code because there are better ways to decode keyboard information.

Extended Key Flag

The Extended Key Flag is 1 if the keystroke results from one of the additional keys on the IXC Enhanced Keyboard. (The IXC Enhanced Keyboard has function keys across the top and a separate [combined] keypad for cursor keys and number keys.) This flag is set to 1 for the Alt and Ctrl keys at the right of the keyboard, the cursor movement keys (including Insert and Delete) that are not part of the numeric keypad, the Slash (/) and Enter keys on the numeric keypad, and the Num Lock key. Windows programs generally ignore the Extended Key Flag.

Context Code

The Context Code is 1 if the Alt key is pressed. This bit will always be 1 for the WM_SYSKEYUP and WM_SYSKEYDOWN messages and 0 for the WM_KEYUP and WM_KEYDOWN messages with two exceptions:

If the active window is an icon, it does not have the input focus. All keystrokes generate WM_SYSKEYUP and WM_SYSKEYDOWN messages. If the Alt key is not pressed, the Context Code field is set to 0. (Windows uses SYS keyboard messages so that the active window that is an icon doesn't process these keystrokes.)

On some foreign-language keyboards, certain characters are generated by combining Shift, Ctrl, or Alt with another key. In these cases the lParam variable that accompanies WM_KEYUP and WM_KEYDOWN messages has a 1 in the Context Code field, but the messages are not system keystroke messages.

Previous Key State

The Previous Key State is 0 if the key was previously up and 1 if the key was previously down. It is always set to 1 for a WM_KEYUP or WM_SYSKEYUP message, but it can be 0 or 1 for a WM_KEYDOWN or WM_SYSKEYDOWN message. A 1 indicates second and subsequent messages for keys that are the result of typematic action.

Transition State

The Transition State is 0 if the key is being pressed and 1 if the key is being released. The field is set to 0 for a WM_KEYDOWN or WM_SYSKEYDOWN message and to 1 for a WM_KEYUP or WM_SYSKEYUP.