Detecting simulated keyboard/mouse input

The only way to distinguish between “real” input and “simulated” input (assuming it is being generated with keybd_event()/mouse_event() or SendInput()) is to use a low-level keyboard/mouse hook via SetWindowsHookEx(). The WH_KEYBOARD_LL and WH_MOUSE_LL hook callbacks provide INJECTED flags for simulated input.

Simulate keyboard input in C#

Have you tried using SendInput which supersedes keybd_event? To call SendInput from C#, you’re going to need to create a whole bunch of structs. Fortunately, most of this information can be gleaned from pinvoke.net. Looking at the pinvoke documentation for SendInput and the INPUT struct, I came up with the following. The method I’ve written … Read more