On a program that I'm developing I have to simulate keystrokes, and to do so I use the SendInput()
method, passing as argument a vector containing the inputs which are part of the keystroke. My current code seems to work properly with all the combinations I'm testing, except Alt codes.
This is what I do currently:
// Press ALT
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_LMENU;
input.ki.wScan = 0;
input.ki.dwFlags = 0;
input.ki.time = 0;
input.ki.dwExtraInfo = 0;
keystroke.push_back(input);
// Press NumPad2
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_NUMPAD2;
input.ki.wScan = 0;
input.ki.dwFlags = 0;
input.ki.time = 0;
input.ki.dwExtraInfo = 0;
keystroke.push_back(input);
// Release NumPad2
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_NUMPAD2;
input.ki.wScan = 0;
input.ki.dwFlags = KEYEVENTF_KEYUP;
input.ki.time = 0;
input.ki.dwExtraInfo = 0;
keystroke.push_back(input);
// Press NumPad1
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_NUMPAD1;
input.ki.wScan = 0;
input.ki.dwFlags = 0;
input.ki.time = 0;
input.ki.dwExtraInfo = 0;
keystroke.push_back(input);
// Release NumPad1
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_NUMPAD1;
input.ki.wScan = 0;
input.ki.dwFlags = KEYEVENTF_KEYUP;
input.ki.time = 0;
input.ki.dwExtraInfo = 0;
keystroke.push_back(input);
// Press NumPad2
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_NUMPAD2;
input.ki.wScan = 0;
input.ki.dwFlags = 0;
input.ki.time = 0;
input.ki.dwExtraInfo = 0;
keystroke.push_back(input);
// Release NumPad2
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_NUMPAD2;
input.ki.wScan = 0;
input.ki.dwFlags = KEYEVENTF_KEYUP;
input.ki.time = 0;
input.ki.dwExtraInfo = 0;
keystroke.push_back(input);
// Release ALT
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_LMENU;
input.ki.wScan = 0;
input.ki.dwFlags = KEYEVENTF_KEYUP;
input.ki.time = 0;
input.ki.dwExtraInfo = 0;
keystroke.push_back(input);
SendInput(keystroke.size(), &keystroke[0], sizeof(keystroke[0]));
The push_back
s are done in a for
cycle, that's why I entirely redefine the input
variable everytime.
This approach seems to work for every combination excluding Alt codes. How can I make them work too? Thank you.
PS: As you can notice, dwFlags
never declares ALT (VK_LMENU)
as an ExtendedKey since from my understanding only VK_RMENU
(and not VK_LMENU
) is such. This MSDN page seems to confirm so.