Processing Keyboard Messages (Windows Embedded CE 6.0)
1/6/2010
A window receives keyboard input in the form of keystroke messages and character messages. Keystroke messages control window behavior and character messages determine the text that is displayed in a window.
Windows Embedded CE generates a WM_KEYDOWN or a WM_SYSKEYDOWN message when the user presses a key. If the user holds a key down long enough to start automatic repeat functionality, the system generates repeated WM_KEYDOWN or WM_SYSKEYDOWN messages. When the user releases a key, the system generates a WM_KEYUP or a WM_SYSKEYUP message.
The system makes a distinction between system keystrokes and nonsystem keystrokes. System keystrokes produce system keystroke messages, such as WM_SYSKEYDOWN and WM_SYSKEYUP. Nonsystem keystrokes produce nonsystem keystroke messages, such as WM_KEYDOWN and WM_KEYUP.
A system keystroke message is generated when the user types a key in combination with the ALT key or when the user types a key and the focus is NULL. If the focus is NULL, the keyboard event is delivered to the active window. A system keystroke message has the WM_SYS prefix in the message name. A system keystroke message is used primarily by the system rather than by an application. The system uses such a message to provide its built-in keyboard interface to menus and to enable the user to control which window is active. If a window procedure processes a system keyboard message, the window procedure should pass the message to the DefWindowProc function. Otherwise, all system operations that involve the ALT key are disabled whenever that window has the keyboard focus.
The window procedure of the window that has the keyboard focus receives all keystroke messages. However, an application that responds to keyboard input typically processes WM_KEYDOWN messages only.
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 contained in the wParam parameter of the message.
The lParam parameter of a keystroke message contains additional data about the keystroke that generated the message. The following table shows the additional keystroke data that is required by the lParam parameter.
Data | Description |
---|---|
Context code |
The value is 1 if the ALT key was pressed or 0 if the pressed key was released. |
Previous key state |
The value is 1 if the pressed key was previously down or 0 if the pressed key was previously up. The value is 1 for WM_KEYDOWN and WM_SYSKEYDOWN keystroke messages that were generated by automatic repeat functionality. |
Repeat count |
Specifies the number of times that the keystroke was repeated as a result of the user holding down the key. |
Scan code |
Gives the hardware-dependent key scan code. |
Transition state |
The value is 1 if the key was released or if the key was pressed. |
Typically, an application processes only the keystrokes that are generated by noncharacter keys. The following code example shows the window procedure framework that a typical application uses to receive and process keystroke messages.
case WM_KEYDOWN:
switch (wParam)
{
case VK_HOME:
// Insert code here to process the HOME key
// ...
break;
case VK_END:
// Insert code here to process the END key
// ...
break;
case VK_INSERT:
// Insert code here to process the INS key
// ...
break;
case VK_F2:
// Insert code here to process the F2 key
// ...
break;
case VK_LEFT:
// Insert code here to process the LEFT ARROW key
// ...
break;
case VK_RIGHT:
// Insert code here to process the RIGHT ARROW key
// ...
break;
case VK_UP:
// Insert code here to process the UP ARROW key
// ...
break;
case VK_DOWN:
// Insert code here to process the DOWN ARROW key
// ...
break;
case VK_DELETE:
// Insert code here to process the DELETE key
// ...
break;
default:
// Insert code here to process other noncharacter keystrokes
// ...
break;
}