Search code examples
.netwinapihookkeyboard-hooksetwindowshookex

Can I use Global Hooks and Raw Input at the same time?


I tried using Global Hooks using SetWindowsHookEx to get all the the keystrokes.

Problem is i cant use Global Hooks and Raw Input at the same time.

I'm thinking there must be some issue here because the Global Hooks automatically gets disabled after I enable Raw Inputs.


Solution

  • Who told you that they needed to be used together? If your application is registered to handle raw input, there's absolutely no reason to install a global hook. The whole point of the raw input model is for an application to receive notification of and process raw input from any HID connected to the computer.

    Rather, you listen for the WM_INPUT message, which is sent to the application's message queue for any HID that you've registered by calling the RegisterRawInputDevices function. Upon receipt of this message, your application should call the GetRawInputData function using the RAWINPUT handle contained in the lParam parameter of the WM_INPUT message. Sample code is available here.

    Alternatively, you can do a buffered read of the raw data. This is more useful for devices that generate large amounts of input at a time. With this approach, you call the GetRawInputBuffer function, which returns an array of RAWINPUT structures. Again, sample code is available here.

    Topical reading on the Raw Input functions is here on MSDN.