I was making a program to send keystrokes to another window and I got it all working, but I had to go online and find a function to do the keystroke portion itself. It works but I have no idea what it is actually doing. Can someone comment each line of this function explaining what it's doing?
void GenerateKey(int vk, BOOL bExtended) {
KEYBDINPUT kb = {0};
INPUT Input = {0};
/* Generate a "key down" */
if (bExtended) { kb.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));
/* Generate a "key up" */
ZeroMemory(&kb, sizeof(KEYBDINPUT));
ZeroMemory(&Input, sizeof(INPUT));
kb.dwFlags = KEYEVENTF_KEYUP;
if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));
return;
}
Here's an example of a call to it:
GenerateKey('C', FALSE); // Sends keystroke 'c'
This sends the keystroke 'c'.
This function only works with capital letters and seems to only work with specific hex codes. For example, to send a carriage return (enter key), here is the call:
GenerateKey(0x0D, FALSE); // Sends carriage return
However, if I try sending a question mark (hex 0x3F) with either of these calls, nothing happens:
GenerateKey(0x3F, FALSE); // Nothing happens
GenerateKey('?', FALSE); // Nothing happens
Can anyone see why those wouldn't work?
Also, can someone explain what the second argument, BOOL bExtended, is for? Changing between TRUE and FALSE seems to make no difference in the keystrokes it sends.