I wanted to write C ++ code to emulate the keyboard key press "A":
// Set up a generic keyboard event. ip.type = INPUT_KEYBOARD; ip.ki.wScan = 0; // hardware scan code for key ip.ki.time = 0; ip.ki.dwExtraInfo = 0; // Press the "..." key ip.ki.wVk = code; // virtual-key code for the "a" key ip.ki.dwFlags = 0; // 0 for key press SendInput(1, &ip, sizeof(INPUT)); // Release the "..." key ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release SendInput(1, &ip, sizeof(INPUT));
It works great when I run another program and wait for my program to execute, clicking “A” and the first program react to it. But I found that in another application my action was somehow prevented (I can manually press "A" on the keyboard, but using my program does not cause any action).
So, what can I do to make pressing "A" from a program more identical to manually pressing "A" (so that the second program does not recognize that it was called from the program)?
I do not have the source code of the second program and I do not know how it recognizes that "A" was not pressed manually.
I am sure that the window in which I want to respond to my code is the foreground, receiving and blocking my key (so that it can decide that this event is not from the user, but from the program).
source share