0% found this document useful (0 votes)
222 views

Keyboard Events Simulation Using Keybd - Event Function - CodeProject

Keyboard Events Simulation

Uploaded by

Luis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
222 views

Keyboard Events Simulation Using Keybd - Event Function - CodeProject

Keyboard Events Simulation

Uploaded by

Luis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

10,726,551 members (65,533 online) Sign in

home articles quick answers discussions features community help Search for articles, questions, tips

Articles » General Reading » Hardware & System » General

Article
Keyboard Events Simulation using keybd_event() function About Article
Browse Code

Bugs / Suggestions Naren Neelamegam, 4 Jun 2004 Rate this: Type Article
4.91 (58 votes) Licence CPOL
Stats

Revisions First Posted 4 Jun 2004


A short description of keybd_event() function for beginners.
Alternatives Views 402,657

Comments (41) Bookmarked 111 times

Introduction VC6 VC7 VC7.1


Win2K WinXP
Simulation of a keyboard input is a well known concept for those who are all familiar with Visual Basic. SendKeys()in
Win2003
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 Visual-Studio MFC

beginners who are all just trying to do something different with VC++. Let us get into the steps… Dev Intermediate

View this article's Function Syntax


Workspace

Fork this Workspace Collapse | Copy Code


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_MULTIPLY 0x6A VK_SHIFT 0x10


VK_ADD 0x6B VK_CONTROL 0x11
VK_SEPARATOR0x6C VK_MENU 0x12
VK_SUBTRACT 0x6D VK_PAUSE 0x13
VK_DECIMAL 0x6E VK_CAPITAL 0x14
VK_DIVIDE 0x6F VK_ESCAPE 0x1B
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 Related Articles
VK_F6 0x75 VK_RIGHT 0x27
VK_F7 0x76 VK_DOWN 0x28 Creating On-Screen Keyboard
Using Attached Behavior in WPF
VK_F8 0x77 VK_PRINT 0x2A
Toggling the Num Lock, Caps
VK_F9 0x78 VK_SNAPSHOT0x2C Lock, and Scroll Lock keys
VK_F10 0x79 VK_INSERT 0x2D Screen Keyboard
VK_F11 0x7A VK_DELETE 0x2E Clipboard little helper
VK_F12 0x7B VK_LWIN 0x5B Mouse and KeyBoard Hooking
VK_NUMLOCK 0x90 VK_RWIN 0x5C utility with VC++

VK_SCROLL 0x91 VK_NUMPAD0 0x60 Read and Update CAPS or NUM


VK_LSHIFT 0xA0 VK_NUMPAD1 0x61 Lock Status from your
Application
VK_RSHIFT 0xA1 VK_NUMPAD2 0x62
Using keyboard hooks in WinCE
VK_LCONTROL 0xA2 VK_NUMPAD3 0x63
Global Mouse and Keyboard
VK_RCONTROL 0xA3 VK_NUMPAD4 0x64 Library
VK_LMENU 0xA4 VK_NUMPAD5 0x65 Toggle Keys Controller
VK_RMENU 0xA5 VK_NUMPAD6 0x66 A class for the automation of
simple computer tasks using
Character key can be converted into virtual key using VkKeyScan(TCHAR ch)function. keybd_event and CreateProcess
Serial Number Paster
bScanCode
TrafficLights
Scan code is the hardware key code for the key (make and break codes). The following are the available scan Keyboard hooks in WinCE
codes (break code will be used in this parameter). Touchscreen Keyboard
UserControl
SendKeys in C++
Onscreen Keyboard
JavaScript Virtual Keyboard
A Touch Screen Keyboard
Control in WPF
Windows Mobile Power
Management
How to use your extra mouse
buttons in games or apps

Related Research

How to Apply Changes to Your


DB2 Database with Minimal Risk

Toad Oracle: Discover the Top


10 Most Useful Toad Features

Data Modeling for Effective


Data Warehousing and Business
Intelligence

Essential Keys to Mobile


Usability
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
If specified, the scan code was preceded by a prefix byte having the value 0xE0
KEYEVENTF_EXTENDEDKEY(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
Collapse | Copy 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)

About the Author

Naren Neelamegam
Software Developer
India

E-Mail : [email protected]
Web : http://www.narenn.com

Article Top

Comments and Discussions

You must Sign In to use this message board.

Search this forum Go

Profile popups Spacing Relaxed Noise Medium Layout Thread View Per page 50 Update

First Prev Next

keyboard opium_21002100 16-Jan-14 20:34 1


MD. Mohiuddin Ahmed 16-Dec-12 6:22 1
My vote of 4

what handle window to simulation ? if no active window tieulamtu2021 18-Sep-12 22:54 1


simulate to where .please help me !

how can i download this code? Member 8112150 27-Jul-11 18:06 1

Sendkeys Modification of Above Code for Citrix Andrewiski 30-Aug-10 5:30 1

How to Use this Example to Send Tab to Citrix Andrewiski 26-Aug-10 10:47 2

Simulate key from service friendship2007 11-Nov-08 15:44 1

NEED HEPL!!!!!!!! sekharsam 9-Jul-08 23:06 1

Need a Help sekharsam 9-Jul-08 20:03 2

keep key pressed SilverV 29-Apr-08 6:08 1

Notebook FN Key snakeyeyess 16-Apr-08 2:46 1

virtual keyboard themancer 12-Sep-07 9:40 1

sendkeys sucks :) nekropatolog 5-Aug-07 23:09 1

A couple of notes on this article CPU Killer 5-Aug-07 12:21 1

How to simulate press and hold a character key? lc123 4-Mar-07 12:04 2

Good article! valmierietis 2-Feb-07 4:04 2

Send ctrl+alt+del seralav 22-Oct-06 21:48 6

To send Unicode character masfoy 9-Mar-05 21:08 1

Gettinh Pressed Keys [modified] [email protected] 4-Aug-04 22:41 3

PageUp and ..Down farG 31-Jul-04 3:46 3

Suggestion! cjwn 13-Jun-04 4:02 1

A nice little surprise Aaron Reginald 6-Jun-04 1:25 2

Right to the point. WREY 5-Jun-04 19:33 2

keybd_event has been superseded Miguel Hasse de Oliveira 5-Jun-04 3:12 3

Last Visit: 31-Dec-99 18:00 Last Update: 11-Jul-14 23:34 Refresh 1

General News Suggestion Question Bug Answer Joke Rant Admin

Permalink | Advertise | Privacy | Mobile Layout: fixed | fluid Article Copyright 2004 by Naren Neelamegam
Web04 | 2.8.140709.1 | Last Updated 5 Jun 2004 Everything else Copyright © CodeProject, 1999-2014
Terms of Service

You might also like