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:
bVirtualKeyVirtual keycode that has to be send as key input. The following are the available predefined virtual key codes:
VK_NUMPAD70x67 VK_BACK0x08 VK_NUMPAD80x68 VK_TAB0x09 VK_NUMPAD90x69 VK_RETURN0x0D VK_MULTIPLY0x6A VK_SHIFT0x10 VK_ADD0x6B VK_CONTROL0x11 VK_SEPARATOR0x6C VK_MENU0x12 VK_SUBTRACT0x6D VK_PAUSE0x13 VK_DECIMAL0x6E VK_CAPITAL0x14 VK_DIVIDE0x6F VK_ESCAPE0x1B VK_F10x70 VK_SPACE0x20 VK_F20x71 VK_END0x23 VK_F30x72 VK_HOME0x24 VK_F40x73 VK_LEFT0x25 VK_F50x74 VK_UP0x26 VK_F60x75 VK_RIGHT0x27 VK_F70x76 VK_DOWN0x28 VK_F80x77 VK_PRINT0x2A VK_F90x78 VK_SNAPSHOT0x2C VK_F100x79 VK_INSERT0x2D VK_F110x7A VK_DELETE0x2E VK_F120x7B VK_LWIN0x5B VK_NUMLOCK0x90 VK_RWIN0x5C VK_SCROLL0x91 VK_NUMPAD00x60 VK_LSHIFT0xA0 VK_NUMPAD10x61 VK_RSHIFT0xA1 VK_NUMPAD20x62 VK_LCONTROL0xA2 VK_NUMPAD30x63 VK_RCONTROL0xA3 VK_NUMPAD40x64 VK_LMENU0xA4 VK_NUMPAD50x65 VK_RMENU0xA5 VK_NUMPAD60x66 Character key can be converted into virtual key using
VkKeyScan(TCHAR ch)function.bScanCodeScan 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).


dwFlagsA 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_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. dwExtraInfo32-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
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.
About Naren Neelamegam
| E-Mail : loveablevirus@yahoo.com Web : http://www.naren.co.nr Click here to view Naren Neelamegam's online profile. |
浙公网安备 33010602011771号