keybd_event Function Home
keybd_event Function

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_EXTENDEDKEY
If specified, the scan code was preceded by a prefix byte having the value 0xE0 (224).
KEYEVENTF_KEYUP
If 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.

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 );
   }

Function Information

Minimum DLL Versionuser32.dll
HeaderDeclared in Winuser.h, include Windows.h
Import libraryUser32.lib
Minimum operating systems Windows 95, Windows NT 3.1

See Also

Community Content

Scan Codes
Added by:JackTripper
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
+ = 0D
Backspace 0E
Tab 0F
Q 10
W 11
E 12
R 13
T 14
Y 15
U 16
I 17
O 18
P 19
{ [ 1A
} ] 1B
| \ 2B
Caps Lock 3A
A 1E
S 1F
D 20
F 21
G 22
H 23
J 24
K 25
L 26
: ; 27
“ ‘ 28
Enter 1C
L SHIFT 2A
Z 2C
X 2D
C 2E
V 2F
B 30
N 31
M 32
< , 33
> . 34
? / 35
R SHIFT 36
L CTRL 1D
L ALT 38
Space Bar 39
R ALT E0 38
R CTRL E0 1D
Insert E0 52
Delete E0 53
L Arrow E0 4B
Home E0 47
End E0 4F
Up Arrow E0 48
Dn Arrow E0 50
Page Up E0 49
Page Down E0 51
R Arrow E0 4D
Num Lock 45
Numeric 7 47
Numeric 4 4B
Numeric 1 4F
Numeric / E0 35
Numeric 8 48
Numeric 5 4C
Numeric 2 50
Numeric 0 52
Numeric * 37
Numeric 9 49
Numeric 6 4D
Numeric 3 51
Numeric . 53
Numeric - 4A
Numeric + 4E
Numeric Enter E0 1C
Esc 1
F1 3B
F2 3C
F3 3D
F4 3E
F5 3F
F6 40
F7 41
F8 42
F9 43
F10 44
F11 57
F12 58
Print Screen ??
Scroll Lock 46
Pause ??
Left Win E0 5B
Right Win E0 5C
Application E0 5D
ACPI Power E0 5E
ACPI Sleep E0 5F
ACPI Wake E0 63
Virtual-Key Codes at MSDN
Added by:NepTunic
http://msdn.microsoft.com/en-us/library/ms645540.aspx
SendInput?
Added by:Gerard.Brazil
Since this function has been superseded, what is the SendInput equivilant of the SetNumLock example?
vb.net syntax
Added by:dmex
<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
C# syntax
Added by:dornif
[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);
Virtual Keyboard Functionality
Added by:Dj BJ
As send_keys can be used to create Virtual Keyboard applications can someone post short code for example:

How to send the letter 'a' to the PREVIOUS window, and not the "keyboard" Form itself?
© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View