Click here to Skip to main content
13,900,980 members
Click here to Skip to main content
Add your own
alternative version


120 bookmarked
Posted 4 Jun 2004
Licenced CPOL

Keyboard Events Simulation using keybd_event() function

, 4 Jun 2004
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:


    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.

    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.

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: PageUp and ..Down Pin
okchan798-May-07 6:14
memberokchan798-May-07 6:14 
GeneralRe: PageUp and ..Down Pin
xxxLegoxx6-Jun-07 15:36
memberxxxLegoxx6-Jun-07 15:36 
GeneralSuggestion! Pin
cjwn13-Jun-04 4:02
membercjwn13-Jun-04 4:02 
GeneralA nice little surprise Pin
Arun Reginald Zaheeruddin6-Jun-04 1:25
memberArun Reginald Zaheeruddin6-Jun-04 1:25 
GeneralRe: A nice little surprise Pin
Naren Neelamegam6-Jun-04 20:57
memberNaren Neelamegam6-Jun-04 20:57 
GeneralRight to the point. Pin
WREY5-Jun-04 19:33
memberWREY5-Jun-04 19:33 
GeneralRe: Right to the point. Pin
Naren Neelamegam6-Jun-04 21:00
memberNaren Neelamegam6-Jun-04 21:00 
Generalkeybd_event has been superseded Pin
Miguel Hasse de Oliveira5-Jun-04 3:12
memberMiguel Hasse de Oliveira5-Jun-04 3:12 
The keybd_event() function has been superseded by SendInput() on Windows 98, Windows NT 4.0 Service Pack 3 and later... this is documented in MSDN.

Miguel Hasse
GeneralRe: keybd_event has been superseded Pin
Dominik Reichl5-Jun-04 6:48
memberDominik Reichl5-Jun-04 6:48 
GeneralRe: keybd_event has been superseded PinPopular
Naren Neelamegam6-Jun-04 21:04
memberNaren Neelamegam6-Jun-04 21:04 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190306.1 | Last Updated 5 Jun 2004
Article Copyright 2004 by Naren Neelamegam
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid