keybd_event function (Windows)

Switch View :
ScriptFree
keybd_event function

Applies to: desktop apps only

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.

Note  This function has been superseded. Use SendInput instead.

Syntax

VOID WINAPI keybd_event(
  __in  BYTE bVk,
  __in  BYTE bScan,
  __in  DWORD dwFlags,
  __in  ULONG_PTR dwExtraInfo
);

Parameters

bVk [in]

Type: BYTE

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 [in]

Type: BYTE

A hardware scan code for the key.

dwFlags [in]

Type: DWORD

Controls various aspects of function operation. This parameter can be one or more of the following values.

ValueMeaning
KEYEVENTF_EXTENDEDKEY
0x0001

If specified, the scan code was preceded by a prefix byte having the value 0xE0 (224).

KEYEVENTF_KEYUP
0x0002

If specified, the key is being released. If not specified, the key is being depressed.

 

dwExtraInfo [in]

Type: ULONG_PTR

An additional value associated with the key stroke.

Return value

This function does not return a 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.

Examples

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


Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winuser.h (include Windows.h)

Library

User32.lib

DLL

User32.dll

See also

Reference
GetAsyncKeyState
GetKeyState
keybd_event
MapVirtualKey
SetKeyboardState
Conceptual
Keyboard Input

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012

Community Content


Thomas Lee
Virtual Keyboard Functionality
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?


[tfl - 24 06 10] Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
PowerShell : http://groups.google.com/group/microsoft.public.windows.powershell/topics?pli=1
SQL Server : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.sqlserver%2C&
Visual Studio : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
Windows : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.windows%2C&
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&


dornif
C# syntax
[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);

dmex
vb.net syntax
<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

Gerard.Brazil
SendInput?
Since this function has been superseded, what is the SendInput equivilant of the SetNumLock example?

JackTripper
Scan Codes
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