C++ – How to track keyboard input and bypass SendInput

ckeyboardwinapiwindows

I'm developing an application which needs to count user keystrokes. It works fine however user can trick the app with SendInput() WINAPI function. Is it any way to differentiate between keystrokes made by real user and those sent via SendInput?

Best Answer

Set a hook with SetWindowsHookEx with the type WH_KEYBOARD_LL. Your callback can inspect the KBDLLHOOKSTRUCT::flags field. If it has the LLKHF_INJECTED flag set, then it's from SentInput or keybd_event. Otherwise, it's from the local keyboard driver.