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 CE generates a WM_KEYDOWN or WM_SYSKEYDOWN message when a user presses a key. If the user holds a key down long enough to start the keyboard repeat feature, the system generates repeated WM_KEYDOWN or WM_SYSKEYDOWN messages. When the user releases a key, the system generates a WM_KEYUP or WM_SYSKEYUP message.
The system makes a distinction between system keystrokes and nonsystem keystrokes. System keystrokes produce system keystroke messages, WM_SYSKEYDOWN and WM_SYSKEYUP. Non-system keystrokes produce nonsystem keystroke messages, WM_KEYDOWN and WM_KEYUP.
System keystroke messages are 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. These messages have the WM_SYS prefix in the message name. System keystroke messages are primarily used by the system rather than by an application. The system uses them 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 system keyboard messages, it should pass the message to the DefWindowProc function. Otherwise, all system operations involving 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 message's wParam parameter.
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 required by the lParam parameter.
Data |
Description |
Repeat count | Specifies the number of times the keystroke was repeated as a result of a user holding down the key. |
Scan code | Gives the hardware-dependent key scan code. |
Context code | The value is 1 if the ALT key was pressed and 0 if the pressed key was released. |
Previous key state | The value is 1 if the pressed key was previously down and 0 if the pressed key was previously up. The value is 1 for WM_KEYDOWN and WM_SYSKEYDOWN keystroke messages that were generated by the automatic repeat feature. |
Transition state | The value is 1 if the key was released or 0 if the key was pressed. |
Typically, an application processes only keystrokes that are generated by non-character 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 DEL key
// ...
break;
default:
// Insert code here to process other non-character keystrokes
// ...
break;
}