|
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 Syntaxvoid keybd_event(BYTE bVirtualKey, BYTE bScanCode,
DWORD dwFlags, DWORD dwExtraInfo);
bVirtualKey
bScanCode
dwFlags
dwExtraInfo
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_SEPARATOR |
0x6C |
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 |
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_LCONTROL |
0xA2 |
VK_NUMPAD3 |
0x63 |
VK_RCONTROL |
0xA3 |
VK_NUMPAD4 |
0x64 |
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
keybd_event(VK_MENU,0xb8,0 , 0);
keybd_event(VK_TAB,0x8f,0 , 0);
keybd_event(VK_TAB,0x8f, KEYEVENTF_KEYUP,0);
keybd_event(VK_MENU,0xb8,KEYEVENTF_KEYUP,0);
keybd_event(VK_CONTROL,0x9d,0 , 0);
keybd_event(VkKeyScan(‘A’),0x9e,0 , 0);
keybd_event(VkKeyScan(‘A’),0x9e, KEYEVENTF_KEYUP,0);
keybd_event(VK_CONTROL,0x9d,KEYEVENTF_KEYUP,0);
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.
| You must Sign In to use this message board. |
|
| | Msgs 1 to 25 of 29 (Total in Forum: 29) (Refresh) | FirstPrevNext |
|
|
 |
|
|
hello friends..........can ani one help me how to lock the windows screen using SendInput Function..........for windows vista
The Newer Function For keydb_event function
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I want to activate the lock screen by windows key and 'L' key......
in windows vista any one help me in doing that....through this keybd_event function.....
thanking u sekhar.................
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
This should keep the left-key pressed until I click button2:
[DllImport("user32.dll")] static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
private void button1_Click(object sender, EventArgs e) { keybd_event(0x26, 0x45, 0, 0); }
private void button2_Click(object sender, EventArgs e) { const int KEYEVENTF_KEYUP = 0x2; keybd_event(0x26, 0x45, KEYEVENTF_KEYUP, 0); }
But it only presses left once. Whats wrong?
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
How can is send the notebook key known as FN? i'm using a wiimote to control the volume and the screen brightness with keybd_event.
Greetz
|
| Sign In·View Thread·PermaLink | 1.40/5 (5 votes) |
|
|
|
 |
|
|
how do a person implement a virtual keyboard in C++.any code would help.
thanks in advance
|
| Sign In·View Thread·PermaLink | 1.50/5 (9 votes) |
|
|
|
 |
|
|
hello
at first i am very sorry for my bad english.
i was must develop one application, which should send keystrokes to the dos application ( classic dos window in win xp) sendkeys() can send keystrokes but, it cant simulated keystrokes between {F1} - {F12} and that was very problem for me.
i solved this problem, for interesting:
private void sndks(int key) { const byte KEYEVENTF_KEYUP = 2;
int ScanKey = MapVirtualKey(key, 0);
keybd_event(0x76, (byte)ScanKey, 0, 0); keybd_event(0x76, (byte)ScanKey, KEYEVENTF_KEYUP, 0); }
KEYEVENTF_KEYUP = 2; release the key
this function send to the dos window any char
regards
|
| Sign In·View Thread·PermaLink | 1.33/5 (2 votes) |
|
|
|
 |
|
|
1. When I was sending the number 6, it actually only worked with the "Make" code versus the "Break" code like the article says. I don't know about letters, I only tried the numbers 6-9.
2. If you don't want to hardcode/look up the scan codes, just use something like this:
<code> void SendKey(char c) { WORD vkey = VkKeyScan(c); BYTE scan = MapVirtualKey(vkey, 0); keybd_event(vkey,scan,0,0); //and of course, if you want to do the whole keypress event keybd_event(vkey,scan,KEYEVENTF_KEYUP,0); } </code>
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
I would like to know how to simlulate press and hold a character key?
For example, if we were to press down a character key, say 'a', on Notepad. 'a' will shows up until we release the key. Is there any way to simulate it?
Thanks for your help.
|
| Sign In·View Thread·PermaLink | 1.25/5 (4 votes) |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
Hello, I´m trying to do an application in c++ which It send the secuence Ctrl+Alt+Del. The code is: keybd_event(VK_LCONTROL,0,0 , 0); keybd_event(VK_MENU,0,0 , 0); keybd_event(VK_DELETE,0,0 , 0);
::Sleep(50);
keybd_event(VK_DELETE,0, KEYEVENTF_KEYUP,0); keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0); keybd_event(VK_LCONTROL,0,KEYEVENTF_KEYUP,0);
but it doesn´t work. Can help me? Thanks.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
keybd_event is not working with ctrl+alt+delete keys simulation. I too agree. Let me have some time to work on it to get a solution. But you can lock your system using WinKey+'L',
keybd_event(VK_LWIN,0, 0 , 0); keybd_event(VkKeyScan('L'),0xa6,0 , 0);
keybd_event(VkKeyScan('L'),0xa6,KEYEVENTF_KEYUP , 0); keybd_event(VK_LWIN,0,KEYEVENTF_KEYUP,0);
or if you want to bring task manager, use "Ctrl+Shift+Esc"
Hope it may help you...
Thanks, Naren Neelamegam
|
| Sign In·View Thread·PermaLink | 3.00/5 (2 votes) |
|
|
|
 |
|
|
 |
|
|
Hi, sir can u tell me how to send a unicode character using keybd_event() function in an application.(of cource vc++6)
Wating for replay.
mahfuz
|
| Sign In·View Thread·PermaLink | 2.00/5 (2 votes) |
|
|
|
 |
|
|
Have you any idea to recognize the key pressed by keyboard whether in my application or in another application ?
massoud.net
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
What with PageUp and PageDown .... Are there virtual keycodes for this keys ?? .... btw - great "code project"
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
Reply is so late.
//PGDN keybd_event(VK_NEXT, 0x22, 0, 0); keybd_event(VK_NEXT,0x22, KEYEVENTF_KEYUP,0);
//PGUP keybd_event(VK_PRIOR , 0x21, 0, 0); keybd_event(VK_PRIOR ,0x21, KEYEVENTF_KEYUP,0);
Hope this help you.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi i have tried implementing the pageup and down method using visual c++ .. however i have gotten a fatal error of LNK 1561: entry point must be defined. here is my code below. i would greatly appreciate it if anyone would lend a helping hand.
private: System::Void PageUp_Click(System::Object^ sender, System::EventArgs^ e) { // Simulate a key press keybd_event( VK_PRIOR, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0 );
// Simulate a key release keybd_event( VK_PRIOR, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
You article is simplyl superb and down-right simple. Keep up the good work! Got my "5".
The beginning of knowledge is the fear of God
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Short and sweet; right to the point!! A lot of articles should take cue from this one.
The only thing missing is a program sample.
Got my '5' anyway.
William
Fortes in fide et opere!
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|