Processing Character Messages
A version of this page is also available for
4/8/2010
When the user enters a character, a character message is not generated automatically; instead, a keystroke message is generated. To translate a keystroke message into a corresponding character message, the application message loop must call the TranslateMessage function. This function examines the virtual key code of a keystroke message and, if the code corresponds to a character, places a character message into the message queue. The character message is removed and dispatched on the next iteration of the message loop.
The message loop should call the TranslateMessage function to translate every message, not just keystroke messages. Although TranslateMessage has no effect on other types of messages, using this function ensures that keyboard input is translated correctly.
There are four character messages: WM_CHAR, WM_SYSCHAR, WM_DEADCHAR, and WM_SYSDEADCHAR. A typical window procedure ignores all of the character messages except for WM_CHAR. The WM_CHAR message contains the character code and the flags that provide additional data about the character. Keys that do not produce printable characters, such as any of the VK_F* keys, will never generate a WM_CHAR message.
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. The character code is in the message wParam parameter.
The following code example shows the window procedure framework that a typical application uses to receive and process character messages and includes the TranslateMessage function in a typical message loop for a thread.
while (GetMessage (&msg, (HWND)NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
See Also
Concepts
Receiving Keyboard Input
Keys and Key Codes for Windows Mobile