Is there a way to detect simulated keyboard/mouse input on Windows. For example, a user types something on his keyboard vs sendKeys/PostMessage/On-screen keyboard. Is there a way that I can distinguish between the two?
EDIT: Perhaps an example would help. I am making a game and want to distinguish between real input vs WinAPI synthesizing keyboard/mouse messages.
The only way to distinguish between "real" input and "simulated" input (assuming it is being generated with
keybd_event()
/mouse_event()
orSendInput()
) is to use a low-level keyboard/mouse hook viaSetWindowsHookEx()
. TheWH_KEYBOARD_LL
andWH_MOUSE_LL
hook callbacks provideINJECTED
flags for simulated input.Starting form Windows 8 there's the
GetCurrentInputMessageSource
function. You can use it, and check theoriginId
enum for the following value:I might be wrong, but the on-screen keyboard (and other applications that simulate user input) most probably uses the SendInput API:
So there is probably no way to tell whether the input is coming from a "real" keyboard or not.