The keybd_event function synthesizes 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.
Windows NT/2000/XP/Vista:This function has been superseded. Use SendInput instead.
Syntax
VOID keybd_event( BYTE bVk, BYTE bScan, DWORD dwFlags, PTR dwExtraInfo );
Parameters
bVk [in] Specifies a virtual-key code. The code must be a value in the range 1 to 254. For a complete list, see Virtual Key Codes. bScan Specifies a hardware scan code for the key.dwFlags [in] Specifies various aspects of function operation. This parameter can be one or more of the following values. KEYEVENTF_EXTENDEDKEYIf specified, the scan code was preceded by a prefix byte having the value 0xE0 (224).KEYEVENTF_KEYUPIf specified, the key is being released. If not specified, the key is being depressed.dwExtraInfo [in] Specifies an additional value associated with the key stroke.
KEYEVENTF_EXTENDEDKEY
KEYEVENTF_KEYUP
Return Value
This function has no return value.
Remarks
An application can simulate a press of the PRINTSCRN key in order to obtain a screen snapshot and save it to the clipboard. To do this, call keybd_event with the bVk parameter set to VK_SNAPSHOT. Windows NT/2000/XP: The keybd_event function can toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK keys. Windows 95/98/Me: The keybd_event function can toggle only the CAPS LOCK and SCROLL LOCK keys. It cannot toggle the NUM LOCK key.The following sample program toggles the NUM LOCK light by using keybd_event() with a virtual key of VK_NUMLOCK. It takes a Boolean value that indicates whether the light should be turned off (FALSE) or on (TRUE). The same technique can be used for the CAPS LOCK key (VK_CAPITAL) and the SCROLL LOCK key (VK_SCROLL). #include <windows.h> void SetNumLock( BOOL bState ) { BYTE keyState[256]; GetKeyboardState((LPBYTE)&keyState); if( (bState && !(keyState[VK_NUMLOCK] & 1)) || (!bState && (keyState[VK_NUMLOCK] & 1)) ) { // Simulate a key press keybd_event( VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0 ); // Simulate a key release keybd_event( VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); } } void main() { SetNumLock( TRUE ); }
An application can simulate a press of the PRINTSCRN key in order to obtain a screen snapshot and save it to the clipboard. To do this, call keybd_event with the bVk parameter set to VK_SNAPSHOT.
Windows NT/2000/XP: The keybd_event function can toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK keys.
Windows 95/98/Me: The keybd_event function can toggle only the CAPS LOCK and SCROLL LOCK keys. It cannot toggle the NUM LOCK key.
The following sample program toggles the NUM LOCK light by using keybd_event() with a virtual key of VK_NUMLOCK. It takes a Boolean value that indicates whether the light should be turned off (FALSE) or on (TRUE). The same technique can be used for the CAPS LOCK key (VK_CAPITAL) and the SCROLL LOCK key (VK_SCROLL).
#include <windows.h> void SetNumLock( BOOL bState ) { BYTE keyState[256]; GetKeyboardState((LPBYTE)&keyState); if( (bState && !(keyState[VK_NUMLOCK] & 1)) || (!bState && (keyState[VK_NUMLOCK] & 1)) ) { // Simulate a key press keybd_event( VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0 ); // Simulate a key release keybd_event( VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); } } void main() { SetNumLock( TRUE ); }
Function Information
Minimum DLL Versionuser32.dllHeaderDeclared in Winuser.h, include Windows.hImport libraryUser32.libMinimum operating systems Windows 95, Windows NT 3.1
See Also
Keyboard Input, GetAsyncKeyState, GetKeyState, keybd_event, MapVirtualKey, SetKeyboardState
Key Scan Code~ ` 29! 1 2@ 2 3# 3 4$ 4 5% 5 6^ 6 7& 7 8* 8 9( 9 0A) 0 0B_ - 0C+ = 0DBackspace 0ETab 0FQ 10W 11E 12R 13T 14Y 15U 16I 17O 18P 19{ [ 1A} ] 1B| \ 2BCaps Lock 3AA 1ES 1FD 20F 21G 22H 23J 24K 25L 26: ; 27“ ‘ 28Enter 1CL SHIFT 2AZ 2CX 2DC 2EV 2FB 30N 31M 32< , 33> . 34? / 35R SHIFT 36L CTRL 1DL ALT 38Space Bar 39R ALT E0 38R CTRL E0 1DInsert E0 52Delete E0 53L Arrow E0 4BHome E0 47End E0 4FUp Arrow E0 48Dn Arrow E0 50Page Up E0 49Page Down E0 51R Arrow E0 4DNum Lock 45Numeric 7 47Numeric 4 4BNumeric 1 4FNumeric / E0 35Numeric 8 48Numeric 5 4CNumeric 2 50Numeric 0 52Numeric * 37Numeric 9 49Numeric 6 4DNumeric 3 51Numeric . 53Numeric - 4ANumeric + 4ENumeric Enter E0 1CEsc 1F1 3BF2 3CF3 3DF4 3EF5 3FF6 40F7 41F8 42F9 43F10 44F11 57F12 58Print Screen ??Scroll Lock 46Pause ??Left Win E0 5BRight Win E0 5CApplication E0 5DACPI Power E0 5EACPI Sleep E0 5FACPI Wake E0 63
<DllImport("user32.dll", EntryPoint:="keybd_event", CharSet:=CharSet.Auto, ExactSpelling:=True)> _ Public Shared Sub Keybd_event(ByVal vk As Byte, ByVal scan As Byte, ByVal flags As Integer, ByVal extrainfo As Integer) End Sub
[DllImport("user32.dll", EntryPoint="keybd_event", CharSet=CharSet.Auto, ExactSpelling=true)] public static extern void Keybd_event(byte vk, byte scan, int flags, int extrainfo);