0

I have 2 keyboards and I want to know if it's possible to know which keyboard generated an input event in Windows (using WINAPI)?

sashoalm
  • 75,001
  • 122
  • 434
  • 781
alexandernst
  • 14,352
  • 22
  • 97
  • 197
  • I removed the Qt tag, there's no cross platform way, I think, so Qt won't have it. Retagged it with winapi instead. Hope it's not a problem. Also see https://gamedev.stackexchange.com/questions/56361/how-do-i-read-input-from-multiple-keyboards-mice-on-one-computer – sashoalm Oct 31 '14 at 15:24
  • @sashoalm That's the whole point of Qt... writing OS-independant code... – alexandernst Oct 31 '14 at 15:29
  • A long time ago I had some success with this project (it uses low level WinAPI calls): http://www.codeproject.com/Articles/17123/Using-Raw-Input-from-C-to-handle-multiple-keyboard – paul Oct 31 '14 at 15:37
  • @alexandernst Yes, but there is no way to do that in Qt. You can only do it in winapi. If you ask "How do I do that in Qt" the answer is "You can't", which won't be very useful. – sashoalm Oct 31 '14 at 15:39
  • @sashoalm Ah, I see what you mean – alexandernst Oct 31 '14 at 16:42
  • @paul Is there a C++ port of that library? Or maybe a possibility to use it from C++? IMHO C# is a horrible language and I really don't want to do the entire project with it... – alexandernst Oct 31 '14 at 16:42
  • If it's using winapi then you can rewrite it in C++ of course. – sashoalm Oct 31 '14 at 16:48
  • 1
    Possible duplicate of [How to distinguish Multiple Keyboards in Delphi?](http://stackoverflow.com/q/3060512/292432) Also [Multiple keyboards and low-level hooks](http://stackoverflow.com/q/91234/292432). And many more. – arx Oct 31 '14 at 16:55
  • @alexandernst the code is just a very simple wrapper that makes calls to WinAPI, so it should be possible to just implement that same wrapper (with the same calls) in c++ – paul Oct 31 '14 at 16:57
  • @sashoalm Won't `nativeEventFilter` help me here? – alexandernst Nov 03 '14 at 12:12
  • @alexandernst Sure, but it's ultimately not Qt, but Winapi - that's why there's native in the name. You will still use the native Winapi functions to deal with the events. Ultimately, your question is Winapi-specific, not Qt-specific. – sashoalm Nov 03 '14 at 13:34
  • @sashoalm Do you happen to have any links to an example how to filter the messages received in `nativeEventFilter`? To what should I cast the `msg` parameter? – alexandernst Nov 03 '14 at 14:10
  • @alexander: Remy's answer provides all the information you are asking for. Whether you place the code in Qt's `nativeEventFilter` or a pure Windows API application's window procedure makes no difference. And since you were confused about the Windows API's interface: All services are provided through an interface callable from C; it does not offer a .NET interface (although a wrapper is trivially easy to implement). – IInspectable Nov 03 '14 at 17:27

1 Answers1

14

Windows has a RAW Input API that can be used to monitor events from keyboards, mice, and HIDs (joysticks, etc) without using low-level hooks.

Use GetRawInputDeviceList() and GetRawInputDeviceInfo() to discover which keyboard devices are connected to the system.

Use RegisterRawInputDevices() to register for events from the desired keyboard devices.

You will receive a WM_INPUT message whenever an input event occurs on a registered device. It will tell you which device sent it.

There is also a WM_INPUT_DEVICE_CHANGE message to notify you when devices are added and removed.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Why was this downvoted? This is a valid and correct answer for the question asked. Microsoft introduced the RAW Input API specifically to allow apps to monitor events from multiple hardwares (with an emphasis on keyboards and mice, but any HID-compatible device will also work, such as joysticks) without having to use low-level hooks. – Remy Lebeau Oct 31 '14 at 17:53
  • I'd guess the downvote was, because you didn't provide a Qt-solution. Since Qt does not offer services to implement the requested functionality it may be a good idea to explicitly state this in your answer. At any rate, shooting the messenger is never a wise idea. – IInspectable Nov 03 '14 at 17:31
  • @IInspectable: when I posted my answer, there was no mention of Qt in the question, and still isn't. Any reference to Qt was removed 2 hours before I posted. – Remy Lebeau Nov 03 '14 at 17:51