Keyboard Input Overview

July 15, 2025 · View on GitHub

Applications should accept user input from the keyboard as well as from the mouse. An application receives keyboard input in the form of messages posted to its windows.

Keyboard Input Model

The system provides device-independent keyboard support for applications by installing a keyboard device driver appropriate for the current keyboard. The system provides language-independent keyboard support by using the language-specific keyboard layout currently selected by the user or the application. The keyboard device driver receives scan codes from the keyboard, which are sent to the keyboard layout where they are translated into messages and posted to the appropriate windows in your application.

Assigned to each key on a keyboard is a unique value called a scan code, a device-dependent identifier for the key on the keyboard. A keyboard generates two scan codes when the user types a key—one when the user presses the key and another when the user releases the key.

The keyboard device driver interprets a scan code and translates (maps) it to a virtual-key code, a device-independent value defined by the system that identifies the purpose of a key. After translating a scan code, the keyboard layout creates a message that includes the scan code, the virtual-key code, and other information about the keystroke, and then places the message in the system message queue. The system removes the message from the system message queue and posts it to the message queue of the appropriate thread. Eventually, the thread's message loop removes the message and passes it to the appropriate window procedure for processing. The following figure illustrates the keyboard input model.

keyboard input processing model

Keyboard Focus and Activation

The system posts keyboard messages to the message queue of the foreground thread that created the window with the keyboard focus. The keyboard focus is a temporary property of a window. The system shares the keyboard among all windows on the display by shifting the keyboard focus, at the user's direction, from one window to another. The window that has the keyboard focus receives (from the message queue of the thread that created it) all keyboard messages until the focus changes to a different window.

A thread can call the GetFocus function to determine which of its windows (if any) currently has the keyboard focus. A thread can give the keyboard focus to one of its windows by calling the SetFocus function. When the keyboard focus changes from one window to another, the system sends a WM_KILLFOCUS message to the window that has lost the focus, and then sends a WM_SETFOCUS message to the window that has gained the focus.

The concept of keyboard focus is related to that of the active window. The active window is the top-level window the user is currently working with. The window with the keyboard focus is either the active window, or a child window of the active window. To help the user identify the active window, the system places it at the top of the Z order and highlights its title bar (if it has one) and border.

The user can activate a top-level window by clicking it, selecting it using the Alt+Tab or Alt+Esc key combination, or selecting it from the Task List. A thread can activate a top-level window by using the SetActiveWindow function. It can determine whether a top-level window it created is active by using the GetActiveWindow function.

When one window is deactivated and another activated, the system sends the WM_ACTIVATE message. The low-order word of the wParam parameter is zero if the window is being deactivated and nonzero if it is being activated. When the default window procedure receives the WM_ACTIVATE message, it sets the keyboard focus to the active window.

To block keyboard and mouse input events from reaching applications, use BlockInput. Note, the BlockInput function will not interfere with the asynchronous keyboard input-state table. This means that calling the SendInput function while input is blocked will change the asynchronous keyboard input-state table.

Keystroke Messages

Pressing a key causes a WM_KEYDOWN or WM_SYSKEYDOWN message to be placed in the thread message queue attached to the window that has the keyboard focus. Releasing a key causes a WM_KEYUP or WM_SYSKEYUP message to be placed in the queue.

Key-up and key-down messages typically occur in pairs, but if the user holds down a key long enough to start the keyboard's automatic repeat feature, the system generates a number of WM_KEYDOWN or WM_SYSKEYDOWN messages in a row. It then generates a single WM_KEYUP or WM_SYSKEYUP message when the user releases the key.

This section covers the following topics:

System and Nonsystem Keystrokes

The system makes a distinction between system keystrokes and nonsystem keystrokes. System keystrokes produce system keystroke messages, WM_SYSKEYDOWN and WM_SYSKEYUP. Nonsystem keystrokes produce nonsystem keystroke messages, WM_KEYDOWN and WM_KEYUP.

If your window procedure must process a system keystroke message, make sure that after processing the message the procedure passes it to the DefWindowProc function. Otherwise, all system operations involving the Alt key will be disabled whenever the window has the keyboard focus. That is, the user won't be able to access the window's menus or System menu, or use the Alt+Esc or Alt+Tab keystroke to activate a different window.

System keystroke messages are primarily for use by the system rather than by an application. The system uses them to provide its built-in keyboard interface to menus and to allow the user to control which window is active. System keystroke messages are generated when the user types a key in combination with the Alt key, or when the user types and no window has the keyboard focus (for example, when the active application is minimized). In this case, the messages are posted to the message queue attached to the active window.

Nonsystem keystroke messages are for use by application windows; the DefWindowProc function does nothing with them. A window procedure can discard any nonsystem keystroke messages that it does not need.

Virtual-Key Codes Described

The wParam parameter of a keystroke message contains the virtual-key code of the key that was pressed or released. A window procedure processes or ignores a keystroke message, depending on the value of the virtual-key code.

A typical window procedure processes only a small subset of the keystroke messages that it receives and ignores the rest. For example, a window procedure might process only WM_KEYDOWN keystroke messages, and only those that contain virtual-key codes for the cursor movement keys, shift keys (also called control keys), and function keys. A typical window procedure does not process keystroke messages from character keys. Instead, it uses the TranslateMessage function to convert the message into character messages. For more information about TranslateMessage and character messages, see Character Messages.

Keystroke Message Flags

The lParam parameter of a keystroke message contains additional information about the keystroke that generated the message. This information includes the repeat count, the scan code, the extended-key flag, the context code, the previous key-state flag, and the transition-state flag. The following illustration shows the locations of these flags and values in the lParam parameter.

the locations of the flags and values in the lparam parameter of a keystroke message

An application can use the following values to get the keystroke flags from high-order word of the lParam.

ValueDescription
KF_EXTENDED
0x0100
Manipulates the extended key flag.
KF_DLGMODE
0x0800
Manipulates the dialog mode flag, which indicates whether a dialog box is active.
KF_MENUMODE
0x1000
Manipulates the menu mode flag, which indicates whether a menu is active.
KF_ALTDOWN
0x2000
Manipulates the context code flag.
KF_REPEAT
0x4000
Manipulates the previous key state flag.
KF_UP
0x8000
Manipulates the transition state flag.

Example code:

case WM_KEYDOWN:
case WM_KEYUP:
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
{
    WORD vkCode = LOWORD(wParam);                                 // virtual-key code
    
    WORD keyFlags = HIWORD(lParam);

    WORD scanCode = LOBYTE(keyFlags);                             // scan code
    BOOL isExtendedKey = (keyFlags & KF_EXTENDED) == KF_EXTENDED; // extended-key flag, 1 if scancode has 0xE0 prefix
    
    if (isExtendedKey)
        scanCode = MAKEWORD(scanCode, 0xE0);

    BOOL wasKeyDown = (keyFlags & KF_REPEAT) == KF_REPEAT;        // previous key-state flag, 1 on autorepeat
    WORD repeatCount = LOWORD(lParam);                            // repeat count, > 0 if several keydown messages was combined into one message

    BOOL isKeyReleased = (keyFlags & KF_UP) == KF_UP;             // transition-state flag, 1 on keyup

    // if we want to distinguish these keys:
    switch (vkCode)
    {
    case VK_SHIFT:   // converts to VK_LSHIFT or VK_RSHIFT
    case VK_CONTROL: // converts to VK_LCONTROL or VK_RCONTROL
    case VK_MENU:    // converts to VK_LMENU or VK_RMENU
        vkCode = LOWORD(MapVirtualKeyW(scanCode, MAPVK_VSC_TO_VK_EX));
        break;
    }

    // ...
}
break;

Repeat Count

You can check the repeat count to determine whether a keystroke message represents more than one keystroke. The system increments the count when the keyboard generates WM_KEYDOWN or WM_SYSKEYDOWN messages faster than an application can process them. This often occurs when the user holds down a key long enough to start the keyboard's automatic repeat feature. Instead of filling the system message queue with the resulting key-down messages, the system combines the messages into a single key down message and increments the repeat count. Releasing a key cannot start the automatic repeat feature, so the repeat count for WM_KEYUP and WM_SYSKEYUP messages is always set to 1.

Scan Codes

:::image type="content" source="images/keyboard-key-locations.png" alt-text="Diagram of a Type 4 keyboard with the key locations for each key.":::

The scan code is the value that the system generates when the user presses a key. It is a value that identifies the key pressed regardless of the active keyboard layout, as opposed to the character represented by the key. An application typically ignores scan codes. Instead, it uses the virtual-key codes to interpret keystroke messages.

Modern keyboards are using Human Interface Devices (HID) specification to communicate with a computer. Keyboard driver converts reported HID Usage values sent from the keyboard to scan codes and passes them on to applications.

Note

While virtual key codes are typically more useful to desktop applications, scan codes might be required in specific cases when you need to know which key is pressed regardless of the current keyboard layout. For example, the WASD (W is up, A is left, S is down, and D is right) key bindings for games, which ensure a consistent key formation across US QWERTY or French AZERTY keyboard layouts.

The following table lists the set of Scan Codes as presently recognized by Windows. HID Usage Page/HID Usage ID/HID Usage Name values reference the HID Usage Tables document. The Key Location values reference the preceding keyboard image.

The Scan 1 Make code is delivered in WM_KEYDOWN/WM_KEYUP/WM_SYSKEYDOWN/WM_SYSKEYUP and WM_INPUT messages.

HID Usage Page NameHID Usage NameHID Usage PageHID Usage IDScan 1 MakeKey Location
Generic DesktopSystem Power Down0x00010x00810xE05E
Generic DesktopSystem Sleep0x00010x00820xE05F
Generic DesktopSystem Wake Up0x00010x00830xE063
Keyboard/KeypadErrorRollOver0x00070x00010x00FF
Keyboard/KeypadKeyboard A0x00070x00040x001E31
Keyboard/KeypadKeyboard B0x00070x00050x003050
Keyboard/KeypadKeyboard C0x00070x00060x002E48
Keyboard/KeypadKeyboard D0x00070x00070x002033
Keyboard/KeypadKeyboard E0x00070x00080x001219
Keyboard/KeypadKeyboard F0x00070x00090x002134
Keyboard/KeypadKeyboard G0x00070x000A0x002235
Keyboard/KeypadKeyboard H0x00070x000B0x002336
Keyboard/KeypadKeyboard I0x00070x000C0x001724
Keyboard/KeypadKeyboard J0x00070x000D0x002437
Keyboard/KeypadKeyboard K0x00070x000E0x002538
Keyboard/KeypadKeyboard L0x00070x000F0x002639
Keyboard/KeypadKeyboard M0x00070x00100x003252
Keyboard/KeypadKeyboard N0x00070x00110x003151
Keyboard/KeypadKeyboard O0x00070x00120x001825
Keyboard/KeypadKeyboard P0x00070x00130x001926
Keyboard/KeypadKeyboard Q0x00070x00140x001017
Keyboard/KeypadKeyboard R0x00070x00150x001320
Keyboard/KeypadKeyboard S0x00070x00160x001F32
Keyboard/KeypadKeyboard T0x00070x00170x001421
Keyboard/KeypadKeyboard U0x00070x00180x001623
Keyboard/KeypadKeyboard V0x00070x00190x002F49
Keyboard/KeypadKeyboard W0x00070x001A0x001118
Keyboard/KeypadKeyboard X0x00070x001B0x002D47
Keyboard/KeypadKeyboard Y0x00070x001C0x001522
Keyboard/KeypadKeyboard Z0x00070x001D0x002C46
Keyboard/KeypadKeyboard 1 and Bang0x00070x001E0x00022
Keyboard/KeypadKeyboard 2 and At0x00070x001F0x00033
Keyboard/KeypadKeyboard 3 And Hash0x00070x00200x00044
Keyboard/KeypadKeyboard 4 and Dollar0x00070x00210x00055
Keyboard/KeypadKeyboard 5 and Percent0x00070x00220x00066
Keyboard/KeypadKeyboard 6 and Caret0x00070x00230x00077
Keyboard/KeypadKeyboard 7 and Ampersand0x00070x00240x00088
Keyboard/KeypadKeyboard 8 and Star0x00070x00250x00099
Keyboard/KeypadKeyboard 9 and Left Bracket0x00070x00260x000A10
Keyboard/KeypadKeyboard 0 and Right Bracket0x00070x00270x000B11
Keyboard/KeypadKeyboard Return Enter0x00070x00280x001C43
Keyboard/KeypadKeyboard Escape0x00070x00290x0001110
Keyboard/KeypadKeyboard Delete0x00070x002A0x000E15
Keyboard/KeypadKeyboard Tab0x00070x002B0x000F16
Keyboard/KeypadKeyboard Spacebar0x00070x002C0x003961
Keyboard/KeypadKeyboard Dash and Underscore0x00070x002D0x000C12
Keyboard/KeypadKeyboard Equals and Plus0x00070x002E0x000D13
Keyboard/KeypadKeyboard Left Brace0x00070x002F0x001A27
Keyboard/KeypadKeyboard Right Brace0x00070x00300x001B28
Keyboard/KeypadKeyboard Backslash and Pipe0x00070x00310x002B29
Keyboard/KeypadKeyboard Non-US Hash and Tilde0x00070x00320x002B42
Keyboard/KeypadKeyboard SemiColon and Colon0x00070x00330x002740
Keyboard/KeypadKeyboard Apostrophe and Double Quotation Mark0x00070x00340x002841
Keyboard/KeypadKeyboard Grave Accent and Tilde0x00070x00350x00291
Keyboard/KeypadKeyboard Comma and LessThan0x00070x00360x003353
Keyboard/KeypadKeyboard Period and GreaterThan0x00070x00370x003454
Keyboard/KeypadKeyboard ForwardSlash and QuestionMark0x00070x00380x003555
Keyboard/KeypadKeyboard Caps Lock0x00070x00390x003A30
Keyboard/KeypadKeyboard F10x00070x003A0x003B112
Keyboard/KeypadKeyboard F20x00070x003B0x003C113
Keyboard/KeypadKeyboard F30x00070x003C0x003D114
Keyboard/KeypadKeyboard F40x00070x003D0x003E115
Keyboard/KeypadKeyboard F50x00070x003E0x003F116
Keyboard/KeypadKeyboard F60x00070x003F0x0040117
Keyboard/KeypadKeyboard F70x00070x00400x0041118
Keyboard/KeypadKeyboard F80x00070x00410x0042119
Keyboard/KeypadKeyboard F90x00070x00420x0043120
Keyboard/KeypadKeyboard F100x00070x00430x0044121
Keyboard/KeypadKeyboard F110x00070x00440x0057122
Keyboard/KeypadKeyboard F120x00070x00450x0058123
Keyboard/KeypadKeyboard PrintScreen0x00070x00460xE037
0x0054 *Note 1
124
Keyboard/KeypadKeyboard Scroll Lock0x00070x00470x0046125
Keyboard/KeypadKeyboard Pause0x00070x00480xE11D45
0xE046 *Note 2
0x0045 *Note 3
126
Keyboard/KeypadKeyboard Insert0x00070x00490xE05275
Keyboard/KeypadKeyboard Home0x00070x004A0xE04780
Keyboard/KeypadKeyboard PageUp0x00070x004B0xE04985
Keyboard/KeypadKeyboard Delete Forward0x00070x004C0xE05376
Keyboard/KeypadKeyboard End0x00070x004D0xE04F81
Keyboard/KeypadKeyboard PageDown0x00070x004E0xE05186
Keyboard/KeypadKeyboard RightArrow0x00070x004F0xE04D89
Keyboard/KeypadKeyboard LeftArrow0x00070x00500xE04B79
Keyboard/KeypadKeyboard DownArrow0x00070x00510xE05084
Keyboard/KeypadKeyboard UpArrow0x00070x00520xE04883
Keyboard/KeypadKeypad Num Lock and Clear0x00070x00530x0045
0xE045 *Note 3
90
Keyboard/KeypadKeypad Forward Slash0x00070x00540xE03595
Keyboard/KeypadKeypad Star0x00070x00550x0037100
Keyboard/KeypadKeypad Dash0x00070x00560x004A105
Keyboard/KeypadKeypad Plus0x00070x00570x004E106
Keyboard/KeypadKeypad ENTER0x00070x00580xE01C108
Keyboard/KeypadKeypad 1 and End0x00070x00590x004F93
Keyboard/KeypadKeypad 2 and Down Arrow0x00070x005A0x005098
Keyboard/KeypadKeypad 3 and PageDn0x00070x005B0x0051103
Keyboard/KeypadKeypad 4 and Left Arrow0x00070x005C0x004B92
Keyboard/KeypadKeypad 50x00070x005D0x004C97
Keyboard/KeypadKeypad 6 and Right Arrow0x00070x005E0x004D102
Keyboard/KeypadKeypad 7 and Home0x00070x005F0x004791
Keyboard/KeypadKeypad 8 and Up Arrow0x00070x00600x004896
Keyboard/KeypadKeypad 9 and PageUp0x00070x00610x0049101
Keyboard/KeypadKeypad 0 and Insert0x00070x00620x005299
Keyboard/KeypadKeypad Period and Delete0x00070x00630x0053104
Keyboard/KeypadKeyboard Non-US Backslash and Pipe0x00070x00640x005645
Keyboard/KeypadKeyboard Application0x00070x00650xE05D129
Keyboard/KeypadKeyboard Power0x00070x00660xE05E
Keyboard/KeypadKeypad Equals0x00070x00670x0059
Keyboard/KeypadKeyboard F130x00070x00680x0064
Keyboard/KeypadKeyboard F140x00070x00690x0065
Keyboard/KeypadKeyboard F150x00070x006A0x0066
Keyboard/KeypadKeyboard F160x00070x006B0x0067
Keyboard/KeypadKeyboard F170x00070x006C0x0068
Keyboard/KeypadKeyboard F180x00070x006D0x0069
Keyboard/KeypadKeyboard F190x00070x006E0x006A
Keyboard/KeypadKeyboard F200x00070x006F0x006B
Keyboard/KeypadKeyboard F210x00070x00700x006C
Keyboard/KeypadKeyboard F220x00070x00710x006D
Keyboard/KeypadKeyboard F230x00070x00720x006E
Keyboard/KeypadKeyboard F240x00070x00730x0076
Keyboard/KeypadKeypad Comma0x00070x00850x007E107 *Note 4
Keyboard/KeypadKeyboard International10x00070x00870x007356 *Note 4, 5
Keyboard/KeypadKeyboard International20x00070x00880x0070133 *Note 5
Keyboard/KeypadKeyboard International30x00070x00890x007D14 *Note 5
Keyboard/KeypadKeyboard International40x00070x008A0x0079132 *Note 5
Keyboard/KeypadKeyboard International50x00070x008B0x007B131 *Note 5
Keyboard/KeypadKeyboard International60x00070x008C0x005C
Keyboard/KeypadKeyboard LANG10x00070x00900x0072 *Note 6
0x00F2 *Note 3, 6
Keyboard/KeypadKeyboard LANG20x00070x00910x0071 *Note 6
0x00F1 *Note 3, 6
Keyboard/KeypadKeyboard LANG30x00070x00920x0078
Keyboard/KeypadKeyboard LANG40x00070x00930x0077
Keyboard/KeypadKeyboard LANG50x00070x00940x0076
Keyboard/KeypadKeyboard LeftControl0x00070x00E00x001D58
Keyboard/KeypadKeyboard LeftShift0x00070x00E10x002A44
Keyboard/KeypadKeyboard LeftAlt0x00070x00E20x003860
Keyboard/KeypadKeyboard Left GUI0x00070x00E30xE05B127
Keyboard/KeypadKeyboard RightControl0x00070x00E40xE01D64
Keyboard/KeypadKeyboard RightShift0x00070x00E50x003657
Keyboard/KeypadKeyboard RightAlt0x00070x00E60xE03862
Keyboard/KeypadKeyboard Right GUI0x00070x00E70xE05C128
ConsumerScan Next Track0x000C0x00B50xE019
ConsumerScan Previous Track0x000C0x00B60xE010
ConsumerStop0x000C0x00B70xE024
ConsumerPlay/Pause0x000C0x00CD0xE022
ConsumerMute0x000C0x00E20xE020
ConsumerVolume Increment0x000C0x00E90xE030
ConsumerVolume Decrement0x000C0x00EA0xE02E
ConsumerAL Consumer Control Configuration0x000C0x01830xE06D
ConsumerAL Email Reader0x000C0x018A0xE06C
ConsumerAL Calculator0x000C0x01920xE021
ConsumerAL Local Machine Browser0x000C0x01940xE06B
ConsumerAC Search0x000C0x02210xE065
ConsumerAC Home0x000C0x02230xE032
ConsumerAC Back0x000C0x02240xE06A
ConsumerAC Forward0x000C0x02250xE069
ConsumerAC Stop0x000C0x02260xE068
ConsumerAC Refresh0x000C0x02270xE067
ConsumerAC Bookmarks0x000C0x022A0xE066

Notes:

  1. SysRq key scan code is emitted on Alt+Print screen keystroke
  2. Break key scan code is emitted on Ctrl+Pause keystroke
  3. As seen in legacy keyboard messages
  4. The key is present on Brazilian keyboards
  5. The key is present on Japanese keyboards
  6. The scan code is emitted in key release event only

Extended-Key Flag

The extended-key flag indicates whether the keystroke message originated from one of the additional keys on the Enhanced 101/102-key keyboard. The extended keys consist of the Alt and Ctrl keys on the right-hand side of the keyboard; the Insert*, Delete*, Home, End, Page up, Page down, and Arrow keys in the clusters to the left of the numeric keypad; the Num lock key; the Break (Ctrl+Pause) key; the Print screen key; and the Divide (/) and Enter keys on the numeric keypad. The right-hand Shift key is not considered an extended-key, it has a separate scan code instead.

If specified, the scan code consists of a sequence of two bytes, where the first byte has a value of 0xE0.

Context Code

The context code indicates whether the Alt key was down when the keystroke message was generated. The code is 1 if the Alt key was down and 0 if it was up.

Previous Key-State Flag

The previous key-state flag indicates whether the key that generated the keystroke message was previously up or down. It is 1 if the key was previously down and 0 if the key was previously up. You can use this flag to identify keystroke messages generated by the keyboard's automatic repeat feature. This flag is set to 1 for WM_KEYDOWN and WM_SYSKEYDOWN keystroke messages generated by the automatic repeat feature. It is always set to 1 for WM_KEYUP and WM_SYSKEYUP messages.

Transition-State Flag

The transition-state flag indicates whether pressing a key or releasing a key generated the keystroke message. This flag is always set to 0 for WM_KEYDOWN and WM_SYSKEYDOWN messages; it is always set to 1 for WM_KEYUP and WM_SYSKEYUP messages.

Character Messages

Keystroke messages provide a lot of information about keystrokes, but they do not provide character codes for character keystrokes. To retrieve character codes, an application must include the TranslateMessage function in its thread message loop. TranslateMessage passes a WM_KEYDOWN or WM_SYSKEYDOWN message to the keyboard layout. The layout examines the message's virtual-key code and, if it corresponds to a character key, provides the character code equivalent (taking into account the state of the Shift and Caps Lock keys). It then generates a character message that includes the character code and places the message at the top of the message queue. The next iteration of the message loop removes the character message from the queue and dispatches the message to the appropriate window procedure.

This section covers the following topics:

Nonsystem Character Messages

A window procedure can receive the following character messages: WM_CHAR, WM_DEADCHAR, WM_SYSCHAR, WM_SYSDEADCHAR, and WM_UNICHAR. The TranslateMessage function generates a WM_CHAR or WM_DEADCHAR message when it processes a WM_KEYDOWN message. Similarly, it generates a WM_SYSCHAR or WM_SYSDEADCHAR message when it processes a WM_SYSKEYDOWN message.

An application that processes keyboard input typically ignores all but the WM_CHAR and WM_UNICHAR messages, passing any other messages to the DefWindowProc function. Note that WM_CHAR uses UTF-16 (16-bit Unicode Transformation Format) or ANSI character set while WM_UNICHAR always uses UTF-32 (32-bit Unicode Transformation Format). The system uses the WM_SYSCHAR and WM_SYSDEADCHAR messages to implement menu mnemonics.

The wParam parameter of all character messages contains the character code of the character key that was pressed. The value of the character code depends on the window class of the window receiving the message. If the Unicode version of the RegisterClass function was used to register the window class, the system provides Unicode characters to all windows of that class. Otherwise, the system provides ANSI character codes. For more information, see Registering Window Classes and Use UTF-8 code pages in Windows apps.

The contents of the lParam parameter of a character message are identical to the contents of the lParam parameter of the key-down message that was translated to produce the character message. For information, see Keystroke Message Flags.

Dead-Character Messages

Some non-English keyboards contain character keys that are not expected to produce characters by themselves. Instead, they are used to add a diacritic to the character produced by the subsequent keystroke. These keys are called dead keys. The circumflex key on a German keyboard is an example of a dead key. To enter the character consisting of an "o" with a circumflex, a German user would type the circumflex key followed by the "o" key. The window with the keyboard focus would receive the following sequence of messages:

  1. WM_KEYDOWN
  2. WM_DEADCHAR
  3. WM_KEYUP
  4. WM_KEYDOWN
  5. WM_CHAR
  6. WM_KEYUP

TranslateMessage generates the WM_DEADCHAR message when it processes the WM_KEYDOWN message from a dead key. Although the wParam parameter of the WM_DEADCHAR message contains the character code of the diacritic for the dead key, an application typically ignores the message. Instead, it processes the WM_CHAR message generated by the subsequent keystroke. The wParam parameter of the WM_CHAR message contains the character code of the letter with the diacritic. If the subsequent keystroke generates a character that cannot be combined with a diacritic, the system generates two WM_CHAR messages. The wParam parameter of the first contains the character code of the diacritic; the wParam parameter of the second contains the character code of the subsequent character key.

The TranslateMessage function generates the WM_SYSDEADCHAR message when it processes the WM_SYSKEYDOWN message from a system dead key (a dead key that is pressed in combination with the Alt key). An application typically ignores the WM_SYSDEADCHAR message.

Key Status

While processing a keyboard message, an application may need to determine the status of another key besides the one that generated the current message. For example, a word-processing application that allows the user to press Shift+End to select a block of text must check the status of the Shift key whenever it receives a keystroke message from the End key. The application can use the GetKeyState function to determine the status of a virtual key at the time the current message was generated; it can use the GetAsyncKeyState function to retrieve the current status of a virtual key.

Some keys are considered toggle keys that change the state of the keyboard layout. Toggle keys usually include Caps Lock (VK_CAPITAL), Num Lock (VK_NUMLOCK), and Scroll Lock (VK_SCROLL) keys. Most keyboards have corresponding LED indicators for these keys.

The keyboard layout maintains a list of names. The name of a key that produces a single character is the same as the character produced by the key. The name of a noncharacter key such as Tab and Enter is stored as a character string. An application can retrieve the name of any key from the keyboard layout by calling the GetKeyNameText function.

Keystroke and Character Translations

The system includes several special purpose functions that translate scan codes, character codes, and virtual-key codes provided by various keystroke messages. These functions include MapVirtualKey, ToAscii, ToUnicode, and VkKeyScan.

In addition, Microsoft Rich Edit 3.0 supports the HexToUnicode IME, which allows a user to convert between hexadecimal and Unicode characters by using hot keys. This means that when Microsoft Rich Edit 3.0 is incorporated into an application, the application will inherit the features of the HexToUnicode IME.

Hot-Key Support

A hot key is a key combination that generates a WM_HOTKEY message, a message the system places at the top of a thread's message queue, bypassing any existing messages in the queue. Applications use hot keys to obtain high-priority keyboard input from the user. For example, by defining a hot key consisting of the Ctrl+C keystroke, an application can allow the user to cancel a lengthy operation.

To define a hot key, an application calls the RegisterHotKey function, specifying the combination of keys that generates the WM_HOTKEY message, the handle to the window to receive the message, and the identifier of the hot key. When the user presses the hot key, a WM_HOTKEY message is placed in the message queue of the thread that created the window. The wParam parameter of the message contains the identifier of the hot key. The application can define multiple hot keys for a thread, but each hot key in the thread must have a unique identifier. Before the application terminates, it should use the UnregisterHotKey function to destroy the hot key.

Applications can use a hot key control to make it easy for the user to choose a hot key. Hot key controls are typically used to define a hot key that activates a window; they do not use the RegisterHotKey and UnregisterHotKey functions. Instead, an application that uses a hot key control typically sends the WM_SETHOTKEY message to set the hot key. Whenever the user presses the hot key, the system sends a WM_SYSCOMMAND message specifying SC_HOTKEY. For more information about hot key controls, see "Using Hot Key Controls" in Hot Key Controls.

Keyboard Keys for Browsing and Other Functions

Windows provides support for keyboards with special keys for browser functions, media functions, application launching, and power management. The WM_APPCOMMAND supports the extra keyboard keys. In addition, the ShellProc function is modified to support the extra keyboard keys.

It is unlikely that a child window in a component application will be able to directly implement commands for these extra keyboard keys. So when one of these keys is pressed, DefWindowProc will send a WM_APPCOMMAND message to a window. DefWindowProc will also bubble the WM_APPCOMMAND message to its parent window. This is similar to the way context menus are invoked with the right mouse button, which is that DefWindowProc sends a WM_CONTEXTMENU message on a right button click, and bubbles it to its parent. Additionally, if DefWindowProc receives a WM_APPCOMMAND message for a top-level window, it will call a shell hook with code HSHELL_APPCOMMAND.

Windows also supports the Microsoft IntelliMouse Explorer, which is a mouse with five buttons. The two extra buttons support forward and backward browser navigation. For more information, see XBUTTONs.

Simulating Input

To simulate an uninterrupted series of user input events, use the SendInput function. The function accepts three parameters. The first parameter, cInputs, indicates the number of input events that will be simulated. The second parameter, rgInputs, is an array of INPUT structures, each describing a type of input event and additional information about that event. The last parameter, cbSize, accepts the size of the INPUT structure, in bytes.

The SendInput function works by injecting a series of simulated input events into a device's input stream. The effect is similar to calling the keybd_event or mouse_event function repeatedly, except that the system ensures that no other input events intermingle with the simulated events. When the call completes, the return value indicates the number of input events successfully played. If this value is zero, then input was blocked.

The SendInput function does not reset the keyboard's current state. Therefore, if the user has any keys pressed when you call this function, they might interfere with the events that this function generates. If you are concerned about possible interference, check the keyboard's state with the GetAsyncKeyState function and correct as necessary.

Languages, Locales, and Keyboard Layouts

A language is a natural language, such as English, French, and Japanese. A sublanguage is a variant of a natural language that is spoken in a specific geographical region, such as the English sublanguages spoken in the United Kingdom and the United States. Applications use values, called language identifiers, to uniquely identify languages and sublanguages.

Applications typically use locales to set the language in which input and output is processed. Setting the locale for the keyboard, for example, affects the character values generated by the keyboard. Setting the locale for the display or printer affects the glyphs displayed or printed. Applications set the locale for a keyboard by loading and using keyboard layouts. They set the locale for a display or printer by selecting a font that supports the specified locale.

A keyboard layout not only specifies the physical position of the keys on the keyboard but also determines the character values generated by pressing those keys. Each layout identifies the current input language and determines which character values are generated by which keys and key combinations.

Every keyboard layout has a corresponding handle that identifies the layout and language. The low word of the handle is a language identifier. The high word is a device handle, specifying the physical layout, or is zero, indicating a default physical layout. The user can associate any input language with a physical layout. For example, an English-speaking user who very occasionally works in French can set the input language of the keyboard to French without changing the physical layout of the keyboard. This means the user can enter text in French using the familiar English layout.

Applications are generally not expected to manipulate input languages directly. Instead, the user sets up language and layout combinations, then switches among them. When the user clicks into text marked with a different language, the application calls the ActivateKeyboardLayout function to activate the user's default layout for that language. If the user edits text in a language which is not in the active list, the application can call the LoadKeyboardLayout function with the language to get a layout based on that language.

The ActivateKeyboardLayout function sets the input language for the current task. The hkl parameter can be either the handle to the keyboard layout or a zero-extended language identifier. Keyboard layout handles can be obtained from the LoadKeyboardLayout or GetKeyboardLayoutList function. The HKL_NEXT and HKL_PREV values can also be used to select the next or previous keyboard.

The GetKeyboardLayoutName function retrieves the name of the active keyboard layout for the calling thread. If an application creates the active layout using the LoadKeyboardLayout function, GetKeyboardLayoutName retrieves the same string used to create the layout. Otherwise, the string is the primary language identifier corresponding to the locale of the active layout. This means the function may not necessarily differentiate among different layouts with the same primary language, so cannot return specific information about the input language. The GetKeyboardLayout function, however, can be used to determine the input language.

The LoadKeyboardLayout function loads a keyboard layout and makes the layout available to the user. Applications can make the layout immediately active for the current thread by using the KLF_ACTIVATE value. An application can use the KLF_REORDER value to reorder the layouts without also specifying the KLF_ACTIVATE value. Applications should always use the KLF_SUBSTITUTE_OK value when loading keyboard layouts to ensure that the user's preference, if any, is selected.

For multilingual support, the LoadKeyboardLayout function provides the KLF_REPLACELANG and KLF_NOTELLSHELL flags. The KLF_REPLACELANG flag directs the function to replace an existing keyboard layout without changing the language. Attempting to replace an existing layout using the same language identifier but without specifying KLF_REPLACELANG is an error. The KLF_NOTELLSHELL flag prevents the function from notifying the shell when a keyboard layout is added or replaced. This is useful for applications that add multiple layouts in a consecutive series of calls. This flag should be used in all but the last call.

The UnloadKeyboardLayout function is restricted in that it cannot unload the system default input language. This ensures that the user always has one layout available for enter text using the same character set as used by the shell and file system.