Click here to Skip to main content
12,622,291 members (32,888 online)
Click here to Skip to main content
Add your own
alternative version


1 download
118 bookmarked

Keyboard Events Simulation using keybd_event() function

, 4 Jun 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
A short description of keybd_event() function for beginners.


Simulation of a keyboard input is a well known concept for those who are all familiar with Visual Basic. SendKeys() in Visual Basic does all the things, if you want to do anything without keyboard. But what is in SendKeys() function? What does it do? Can we do such a thing with Visual C++? This article is the answer. I think this will be useful for beginners who are all just trying to do something different with VC++. Let us get into the steps…

Function Syntax

void keybd_event(BYTE bVirtualKey, BYTE bScanCode, 
                         DWORD dwFlags, DWORD dwExtraInfo);
  • bVirtualKey //Virtual Keycode of keys. E.g., VK_RETURN, VK_TAB…
  • bScanCode //Scan Code value of keys. E.g., 0xb8 for “Left Alt” key.
  • dwFlags //Flag that is set for key state. E.g., KEYEVENTF_KEYUP.
  • dwExtraInfo //32-bit extra information about keystroke.

Function Details:

  • bVirtualKey

    Virtual keycode that has to be send as key input. The following are the available predefined virtual key codes:

    VK_NUMPAD7 0x67 VK_BACK 0x08
    VK_NUMPAD8 0x68 VK_TAB 0x09
    VK_NUMPAD9 0x69 VK_RETURN 0x0D
    VK_ADD 0x6B VK_CONTROL 0x11
    VK_F1 0x70 VK_SPACE 0x20
    VK_F2 0x71 VK_END 0x23
    VK_F3 0x72 VK_HOME 0x24
    VK_F4 0x73 VK_LEFT 0x25
    VK_F5 0x74 VK_UP 0x26
    VK_F6 0x75 VK_RIGHT 0x27
    VK_F7 0x76 VK_DOWN 0x28
    VK_F8 0x77 VK_PRINT 0x2A
    VK_F9 0x78 VK_SNAPSHOT 0x2C
    VK_F10 0x79 VK_INSERT 0x2D
    VK_F11 0x7A VK_DELETE 0x2E
    VK_F12 0x7B VK_LWIN 0x5B
    VK_NUMLOCK 0x90 VK_RWIN 0x5C
    VK_SCROLL 0x91 VK_NUMPAD0 0x60
    VK_LSHIFT 0xA0 VK_NUMPAD1 0x61
    VK_RSHIFT 0xA1 VK_NUMPAD2 0x62
    VK_LMENU 0xA4 VK_NUMPAD5 0x65
    VK_RMENU 0xA5 VK_NUMPAD6 0x66

    Character key can be converted into virtual key using VkKeyScan(TCHAR ch) function.

  • bScanCode

    Scan code is the hardware key code for the key (make and break codes). The following are the available scan codes (break code will be used in this parameter).

  • dwFlags

    A set of flag bits that specify various aspects of function operation. An application can use any combination of the following predefined constant values to set the flags.

    Value Meaning
    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

    32-bit extra information along with the keyboard input.

Example Code

// Simulating a Alt+Tab keystroke
keybd_event(VK_MENU,0xb8,0 , 0); //Alt Press
keybd_event(VK_TAB,0x8f,0 , 0); // Tab Press
keybd_event(VK_TAB,0x8f, KEYEVENTF_KEYUP,0); // Tab Release
keybd_event(VK_MENU,0xb8,KEYEVENTF_KEYUP,0); // Alt Release

// Simulating a Ctrl+A keystroke
keybd_event(VK_CONTROL,0x9d,0 , 0); // Ctrl Press
keybd_event(VkKeyScan(‘A’),0x9e,0 , 0); // ‘A’ Press
keybd_event(VkKeyScan(‘A’),0x9e, KEYEVENTF_KEYUP,0); // ‘A’ Release
keybd_event(VK_CONTROL,0x9d,KEYEVENTF_KEYUP,0); // Ctrl Release


This article may not be that much detailed. None of the articles can satisfy one's expectations. But, each article should be a seed for your technical growth. Thus, I believe that this would be a seed. Thank you all.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Naren Neelamegam
Software Developer
United Kingdom United Kingdom
Naren started coding during 1999 with FORTRAN, then COBOL, PASCAL, C, C++, VC++ ..... C#, Java, ASP so on, till today. He claims himself as techie who loves coding, but he is not even sure which technology or platform he loves, most of the time Windows, some time WinCE, some time Linux, nowadays Android and embedded platforms. He can do some query stuffs with Oracle, SQL Server, MySQL. He strongly believes that C/C++ is a must for all programmers, "if you know C/C++, you can do programming on any language". He is an electronic gadget guy who likes to buy small gadgets all the time, at least he will do window shopping on a gadget shop. His interest always have been with Automation in any form, call it a little automated program sitting in his Laptop or a home automation program runs on his mobile. Apart from coding, he likes to do...???

You may also be interested in...


Comments and Discussions

GeneralRe: Need a Help Pin
bryan_f4-Mar-09 4:06
memberbryan_f4-Mar-09 4:06 
Questionkeep key pressed Pin
SilverV29-Apr-08 7:08
memberSilverV29-Apr-08 7:08 
GeneralNotebook FN Key Pin
snakeyeyess16-Apr-08 3:46
membersnakeyeyess16-Apr-08 3:46 
Generalvirtual keyboard Pin
themancer12-Sep-07 10:40
memberthemancer12-Sep-07 10:40 
Generalsendkeys sucks :) Pin
nekropatolog6-Aug-07 0:09
membernekropatolog6-Aug-07 0:09 
GeneralA couple of notes on this article Pin
CPU Killer5-Aug-07 13:21
memberCPU Killer5-Aug-07 13:21 
QuestionHow to simulate press and hold a character key? Pin
lc1234-Mar-07 13:04
memberlc1234-Mar-07 13:04 
AnswerRe: How to simulate press and hold a character key? Pin
BODYPRINT26-Mar-08 1:44
memberBODYPRINT26-Mar-08 1:44 
GeneralGood article! Pin
valmierietis2-Feb-07 5:04
membervalmierietis2-Feb-07 5:04 
GeneralRe: Good article! Pin
valmierietis2-Feb-07 5:09
membervalmierietis2-Feb-07 5:09 
GeneralSend ctrl+alt+del Pin
seralav22-Oct-06 22:48
memberseralav22-Oct-06 22:48 
GeneralRe: Send ctrl+alt+del Pin
Naren Neelamegam27-Nov-06 19:09
memberNaren Neelamegam27-Nov-06 19:09 
GeneralRe: Send ctrl+alt+del Pin
MD. Mohiuddin Ahmed12-Dec-12 20:21
memberMD. Mohiuddin Ahmed12-Dec-12 20:21 
GeneralRe: Send ctrl+alt+del Pin
Member 11829802-Apr-13 23:36
memberMember 11829802-Apr-13 23:36 
GeneralRe: Send ctrl+alt+del Pin
Squat5-Jun-07 20:27
memberSquat5-Jun-07 20:27 
GeneralRe: Send ctrl+alt+del Pin
MD. Mohiuddin Ahmed12-Dec-12 20:17
memberMD. Mohiuddin Ahmed12-Dec-12 20:17 
GeneralTo send Unicode character Pin
masfoy9-Mar-05 22:08
membermasfoy9-Mar-05 22:08 
GeneralGettinh Pressed Keys [modified] Pin
massoud_fnd@yahoo.com4-Aug-04 23:41
membermassoud_fnd@yahoo.com4-Aug-04 23:41 
GeneralRe: Gettinh Pressed Keys Pin
ThatsAlok7-Dec-04 2:11
memberThatsAlok7-Dec-04 2:11 
GeneralRe: Gettinh Pressed Keys Pin
dSolariuM12-Jan-05 19:27
memberdSolariuM12-Jan-05 19:27 
GeneralPageUp and ..Down Pin
farG31-Jul-04 4:46
memberfarG31-Jul-04 4:46 
GeneralRe: PageUp and ..Down Pin
okchan798-May-07 7:14
memberokchan798-May-07 7:14 
GeneralRe: PageUp and ..Down Pin
xxxLegoxx6-Jun-07 16:36
memberxxxLegoxx6-Jun-07 16:36 
GeneralSuggestion! Pin
cjwn13-Jun-04 5:02
membercjwn13-Jun-04 5:02 
GeneralA nice little surprise Pin
Aaron Reginald6-Jun-04 2:25
memberAaron Reginald6-Jun-04 2:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161128.1 | Last Updated 5 Jun 2004
Article Copyright 2004 by Naren Neelamegam
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid