Click here to Skip to main content
15,867,999 members
Articles / Desktop Programming / MFC
Article

Keyboard Events Simulation using keybd_event() function

Rate me:
Please Sign up or sign in to vote.
4.88/5 (65 votes)
4 Jun 2004CPOL2 min read 672.7K   121   42
A short description of keybd_event() function for beginners.

Introduction

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_NUMPAD70x67VK_BACK0x08
    VK_NUMPAD80x68VK_TAB0x09
    VK_NUMPAD90x69VK_RETURN0x0D
    VK_MULTIPLY0x6AVK_SHIFT0x10
    VK_ADD0x6BVK_CONTROL0x11
    VK_SEPARATOR0x6CVK_MENU0x12
    VK_SUBTRACT0x6DVK_PAUSE0x13
    VK_DECIMAL0x6EVK_CAPITAL0x14
    VK_DIVIDE0x6FVK_ESCAPE0x1B
    VK_F10x70VK_SPACE0x20
    VK_F20x71VK_END0x23
    VK_F30x72VK_HOME0x24
    VK_F40x73VK_LEFT0x25
    VK_F50x74VK_UP0x26
    VK_F60x75VK_RIGHT0x27
    VK_F70x76VK_DOWN0x28
    VK_F80x77VK_PRINT0x2A
    VK_F90x78VK_SNAPSHOT0x2C
    VK_F100x79VK_INSERT0x2D
    VK_F110x7AVK_DELETE0x2E
    VK_F120x7BVK_LWIN0x5B
    VK_NUMLOCK0x90VK_RWIN0x5C
    VK_SCROLL0x91VK_NUMPAD00x60
    VK_LSHIFT0xA0VK_NUMPAD10x61
    VK_RSHIFT0xA1VK_NUMPAD20x62
    VK_LCONTROL0xA2VK_NUMPAD30x63
    VK_RCONTROL0xA3VK_NUMPAD40x64
    VK_LMENU0xA4VK_NUMPAD50x65
    VK_RMENU0xA5VK_NUMPAD60x66

    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).

    Image 1

    Image 2

  • 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.

    ValueMeaning
    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

    32-bit extra information along with the keyboard input.

    Image 3

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

Conclusion

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.

License

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


Written By
Software Developer
India India
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...???

Comments and Discussions

 
Suggestion* Pin
Member 1440490421-May-19 0:14
Member 1440490421-May-19 0:14 
Questionkeyboard Pin
opium_2100210016-Jan-14 20:34
opium_2100210016-Jan-14 20:34 
GeneralMy vote of 4 Pin
MD. Mohiuddin Ahmed16-Dec-12 6:22
MD. Mohiuddin Ahmed16-Dec-12 6:22 
Questionwhat handle window to simulation ? if no active window simulate to where .please help me ! Pin
tieulamtu202118-Sep-12 22:54
tieulamtu202118-Sep-12 22:54 
Questionhow can i download this code? Pin
Member 811215027-Jul-11 18:06
Member 811215027-Jul-11 18:06 
GeneralSendkeys Modification of Above Code for Citrix Pin
Andrewiski30-Aug-10 5:30
Andrewiski30-Aug-10 5:30 
QuestionHow to Use this Example to Send Tab to Citrix Pin
Andrewiski26-Aug-10 10:47
Andrewiski26-Aug-10 10:47 
AnswerRe: How to Use this Example to Send Tab to Citrix Pin
Andrewiski26-Aug-10 10:52
Andrewiski26-Aug-10 10:52 
GeneralSimulate key from service Pin
friendship forever11-Nov-08 15:44
friendship forever11-Nov-08 15:44 
GeneralNEED HEPL!!!!!!!! Pin
sekharsam9-Jul-08 23:06
sekharsam9-Jul-08 23:06 
GeneralNeed a Help Pin
sekharsam9-Jul-08 20:03
sekharsam9-Jul-08 20:03 
GeneralRe: Need a Help Pin
bryan_f4-Mar-09 3:06
bryan_f4-Mar-09 3:06 
Questionkeep key pressed Pin
SilverV29-Apr-08 6:08
SilverV29-Apr-08 6:08 
GeneralNotebook FN Key Pin
snakeyeyess16-Apr-08 2:46
snakeyeyess16-Apr-08 2:46 
Generalvirtual keyboard Pin
themancer12-Sep-07 9:40
themancer12-Sep-07 9:40 
Generalsendkeys sucks :) Pin
nekropatolog5-Aug-07 23:09
nekropatolog5-Aug-07 23:09 
GeneralA couple of notes on this article Pin
CPU Killer5-Aug-07 12:21
CPU Killer5-Aug-07 12:21 
QuestionHow to simulate press and hold a character key? Pin
lc1234-Mar-07 12:04
lc1234-Mar-07 12:04 
AnswerRe: How to simulate press and hold a character key? Pin
BODYPRINT26-Mar-08 0:44
BODYPRINT26-Mar-08 0:44 
GeneralGood article! Pin
valmierietis2-Feb-07 4:04
valmierietis2-Feb-07 4:04 
GeneralRe: Good article! Pin
valmierietis2-Feb-07 4:09
valmierietis2-Feb-07 4:09 
GeneralSend ctrl+alt+del Pin
seralav22-Oct-06 21:48
seralav22-Oct-06 21:48 
GeneralRe: Send ctrl+alt+del Pin
Naren Neelamegam27-Nov-06 18:09
Naren Neelamegam27-Nov-06 18:09 
GeneralRe: Send ctrl+alt+del Pin
MD. Mohiuddin Ahmed12-Dec-12 19:21
MD. Mohiuddin Ahmed12-Dec-12 19:21 
GeneralRe: Send ctrl+alt+del Pin
kero_xmemor2-Apr-13 22:36
kero_xmemor2-Apr-13 22:36 

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.