Search code examples
winapikeyboardlow-level

Low level keyboard input from Windows


What win32 calls can be used to detect key press events globally (not just for 1 window, I'd like to get a message EVERY time a key is pressed), from a windows service?


Solution

  • You want to use Win32 Hooks. In particular a keyboard hook.

    You can read more about it here

    The type of hook you want is WH_KEYBOARD and you can set it via the Win32 API SetWindowsHookEx.

    Basically windows will call a function in a dll that you create everytime a key is pressed in any application system wide.

    The hook will call your function which will have this interface:

    LRESULT CALLBACK KeyboardProc(      
        int code,
        WPARAM wParam,
        LPARAM lParam
    );
    

    More information about this callback here.

    With windows hooks you can not only track system wide events across all processes, but you can also filter them and stop them altogether.