Showing posts with label Keypress. Show all posts
Showing posts with label Keypress. Show all posts

Wednesday, December 15, 2010

Simulating keypress

The following code will allow you to simulate the keypress of the keyboard.
This function accepts a string to simulate typing them. It will press the Enter key at the end of the simulation. The code will handle pressing of the "Shift" key when needed.

C++ Source code. MFC.

===============


void SendKeypress(CString strText)
{
int len, i;
char c;
len = strText.GetLength();
for (i = 0; i < len; i++) {
c = strText[i];
WORD vkey = VkKeyScan(c);
WORD shift = HIBYTE(vkey);
BYTE scan = MapVirtualKey(vkey, 0);
if (shift) {
keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT,0), 0, 0);
}
keybd_event(vkey, scan, 0, 0);
keybd_event(vkey, scan, KEYEVENTF_KEYUP, 0);
if (shift) {
keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT,0), KEYEVENTF_KEYUP, 0);
}
}
// Press "Enter"
keybd_event(VK_RETURN, 0x1c, 0 , 0);
keybd_event(VK_RETURN, 0x9c, KEYEVENTF_KEYUP, 0);
}