Keyboard Input

July 15, 2025 ยท View on GitHub

This section describes how the system generates keyboard input and how an application receives and processes that input.

In This Section

NameDescription
About Keyboard InputDiscusses keyboard input.
Using Keyboard InputCovers tasks that are associated with keyboard input.
Keyboard Input ReferenceContains the API reference.

Functions

NameDescription
ActivateKeyboardLayoutSets the input locale identifier (formerly called the keyboard layout handle) for the calling thread or the current process. The input locale identifier specifies a locale as well as the physical layout of the keyboard.
BlockInputBlocks keyboard and mouse input events from reaching applications.
EnableWindowEnables or disables mouse and keyboard input to the specified window or control. When input is disabled, the window does not receive input such as mouse clicks and key presses. When input is enabled, the window receives all input.
GetActiveWindowRetrieves the window handle to the active window attached to the calling thread's message queue.
GetAsyncKeyStateDetermines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState.
GetFocusRetrieves the handle to the window that has the keyboard focus, if the window is attached to the calling thread's message queue.
GetKeyboardLayoutRetrieves the active input locale identifier (formerly called the keyboard layout) for the specified thread. If the idThread parameter is zero, the input locale identifier for the active thread is returned.
GetKeyboardLayoutListRetrieves the input locale identifiers (formerly called keyboard layout handles) corresponding to the current set of input locales in the system. The function copies the identifiers to the specified buffer.
GetKeyboardLayoutNameRetrieves the name of the active input locale identifier (formerly called the keyboard layout).
GetKeyboardStateCopies the status of the 256 virtual keys to the specified buffer.
GetKeyNameTextRetrieves a string that represents the name of a key.
GetKeyStateRetrieves the status of the specified virtual key. The status specifies whether the key is up, down, or toggled (on, off alternating each time the key is pressed).
GetLastInputInfoRetrieves the time of the last input event.
IsWindowEnabledDetermines whether the specified window is enabled for mouse and keyboard input.
LoadKeyboardLayoutLoads a new input locale identifier (formerly called the keyboard layout) into the system. Several input locale identifiers can be loaded at a time, but only one per process is active at a time. Loading multiple input locale identifiers makes it possible to rapidly switch between them.
MapVirtualKeyTranslates (maps) a virtual-key code into a scan code or character value, or translates a scan code into a virtual-key code.
To specify a handle to the keyboard layout to use for translating the specified code, use the MapVirtualKeyEx function.
MapVirtualKeyExMaps a virtual-key code into a scan code or character value, or translates a scan code into a virtual-key code. The function translates the codes using the input language and an input locale identifier.
OemKeyScanMaps OEMASCII codes 0 through 0x0FF into the OEM scan codes and shift states. The function provides information that allows a program to send OEM text to another program by simulating keyboard input.
RegisterHotKeyDefines a system-wide hot key.
SendInputSynthesizes keystrokes, mouse motions, and button clicks.
SetActiveWindowActivates a window. The window must be attached to the calling thread's message queue.
SetFocusSets the keyboard focus to the specified window. The window must be attached to the calling thread's message queue.
SetKeyboardStateCopies a 256-byte array of keyboard key states into the calling thread's keyboard input-state table. This is the same table accessed by the GetKeyboardState and GetKeyState functions. Changes made to this table do not affect keyboard input to any other thread.
ToAsciiTranslates the specified virtual-key code and keyboard state to the corresponding character or characters. The function translates the code using the input language and physical keyboard layout identified by the keyboard layout handle.
To specify a handle to the keyboard layout to use to translate the specified code, use the ToAsciiEx function.
ToAsciiExTranslates the specified virtual-key code and keyboard state to the corresponding character or characters. The function translates the code using the input language and physical keyboard layout identified by the input locale identifier.
ToUnicodeTranslates the specified virtual-key code and keyboard state to the corresponding Unicode character or characters.
To specify a handle to the keyboard layout to use to translate the specified code, use the ToUnicodeEx function.
ToUnicodeExTranslates the specified virtual-key code and keyboard state to the corresponding Unicode character or characters.
UnloadKeyboardLayoutUnloads an input locale identifier (formerly called a keyboard layout).
UnregisterHotKeyFrees a hot key previously registered by the calling thread.
VkKeyScanExTranslates a character to the corresponding virtual-key code and shift state. The function translates the character using the input language and physical keyboard layout identified by the input locale identifier.

The following functions are obsolete.

FunctionDescription
GetKBCodePageRetrieves the current code page.
keybd_eventSynthesizes a keystroke. The system can use such a synthesized keystroke to generate a WM_KEYUP or WM_KEYDOWN message. The keyboard driver's interrupt handler calls the keybd_event function.
VkKeyScanTranslates a character to the corresponding virtual-key code and shift state for the current keyboard.

Messages

NameDescription
WM_GETHOTKEYDetermines the hot key associated with a window.
WM_SETHOTKEYAssociates a hot key with the window. When the user presses the hot key, the system activates the window.

Notifications

NameDescription
WM_ACTIVATESent to both the window being activated and the window being deactivated. If the windows use the same input queue, the message is sent synchronously, first to the window procedure of the top-level window being deactivated, then to the window procedure of the top-level window being activated. If the windows use different input queues, the message is sent asynchronously, so the window is activated immediately.
WM_APPCOMMANDNotifies a window that the user generated an application command event, for example, by clicking an application command button using the mouse or typing an application command key on the keyboard.
WM_CHARPosted to the window with the keyboard focus when a WM_KEYDOWN message is translated by the TranslateMessage function. The WM_CHAR message contains the character code of the key that was pressed.
WM_DEADCHARPosted to the window with the keyboard focus when a WM_KEYUP message is translated by the TranslateMessage function. WM_DEADCHAR specifies a character code generated by a dead key. A dead key is a key that generates a character, such as the umlaut (double-dot), that is combined with another character to form a composite character. For example, the umlaut-O character ( ) is generated by typing the dead key for the umlaut character, and then typing the O key.
WM_HOTKEYPosted when the user presses a hot key registered by the RegisterHotKey function. The message is placed at the top of the message queue associated with the thread that registered the hot key.
WM_KEYDOWNPosted to the window with the keyboard focus when a nonsystem key is pressed. A nonsystem key is a key that is pressed when the ALT key is not pressed.
WM_KEYUPPosted to the window with the keyboard focus when a nonsystem key is released. A nonsystem key is a key that is pressed when the ALT key is not pressed, or a keyboard key that is pressed when a window has the keyboard focus.
WM_KILLFOCUSSent to a window immediately before it loses the keyboard focus.
WM_SETFOCUSSent to a window after it has gained the keyboard focus.
WM_SYSDEADCHARSent to the window with the keyboard focus when a WM_SYSKEYDOWN message is translated by the TranslateMessage function. WM_SYSDEADCHAR specifies the character code of a system dead key that is, a dead key that is pressed while holding down the ALT key.
WM_SYSKEYDOWNPosted to the window with the keyboard focus when the user presses the F10 key (which activates the menu bar) or holds down the ALT key and then presses another key. It also occurs when no window currently has the keyboard focus; in this case, the WM_SYSKEYDOWN message is sent to the active window. The window that receives the message can distinguish between these two contexts by checking the context code in the lParam parameter.
WM_SYSKEYUPPosted to the window with the keyboard focus when the user releases a key that was pressed while the ALT key was held down. It also occurs when no window currently has the keyboard focus; in this case, the WM_SYSKEYUP message is sent to the active window. The window that receives the message can distinguish between these two contexts by checking the context code in the lParam parameter.
WM_UNICHARPosted to the window with the keyboard focus when a WM_KEYDOWN message is translated by the TranslateMessage function. The WM_UNICHAR message contains the character code of the key that was pressed.

Structures

NameDescription
HARDWAREINPUTContains information about a simulated message generated by an input device other than a keyboard or mouse.
INPUTContains information used for synthesizing input events such as keystrokes, mouse movement, and mouse clicks.
KEYBDINPUTContains information about a simulated keyboard event.
LASTINPUTINFOContains the time of the last input.
MOUSEINPUTContains information about a simulated mouse event.

Constants

NameDescription
Virtual-Key CodesThe symbolic constant names, hexadecimal values, and mouse or keyboard equivalents for the virtual-key codes used by the system. The codes are listed in numeric order.

See also