If I wanted to simulate a keypress using an extended scan code like 0xE0 0x1D (for right CTRL), how would I simulate such a keypress in C? I've tried calling SendInput
with two INPUT
structs, but only the left CTRL key was "pressed". The same thing happens in the case of other keys that have a "twin" (Shift and Alt).
Secondly, how would one cause a keyup event for an "extended" key?
The KEYBDINPUT
structure has a KEYEVENTF_EXTENDEDKEY
flag to handle the 0xE0
byte for you:
If specified, the scan code was preceded by a prefix byte that has the value 0xE0 (224).
Try something like this:
INPUT inputs[2];
ZeroMemory(inputs, sizeof(inputs));
inputs[0].type = INPUT_KEYBOARD;
inputs[0].ki.wScan = 0x1D;
inputs[0].ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_EXTENDEDKEY;
CopyMemory(&inputs[1], &inputs[0], sizeof(INPUT));
inputs[1].ki.dwFlags |= KEYEVENTF_KEYUP;
SendInput(2, inputs, sizeof(INPUT));
However, I would suggest using a virtual key instead of a scan code:
INPUT inputs[2];
ZeroMemory(inputs, sizeof(inputs));
inputs[0].type = INPUT_KEYBOARD;
inputs[0].ki.wVk = VK_CONTROL;
inputs[0].ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
CopyMemory(&inputs[1], &inputs[0], sizeof(INPUT));
inputs[1].ki.dwFlags |= KEYEVENTF_KEYUP;
SendInput(2, inputs, sizeof(INPUT));
But, if you absolutely need a scan code, at least have a look at MapVirtualKey()
to convert a virtual key into a scan code:
inputs[0].type = INPUT_KEYBOARD;
inputs[0].ki.wScan = MapVirtualKey(VK_RCONTROL, MAPVK_VK_TO_VSC);
inputs[0].ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_EXTENDEDKEY;