I want to be able to read inputs from multiple keyboards and still differentiate them. It's for split-screen. I know it's possible, because Serious Sam 3 allows it.

Edit : Well a whole lot of my description has been removed, anyway, one of the maine details was that it had to be cross-platform! However, I found just today a POSSIBLE solution, which is using libusb 1.0 (it's Windows-compatible since 1.0), I'm using 1.0.15 (which is actually libusbx, they've created a branch and have come back to the main branch for 1.0.16 =/ )! So my actual answer now is (because I have no PS/2 port :S), is libusb recognizing PS/2 devices (I know nothing about PS/2 standard, so I don't know, but I have some reasons to think it can be recognized). Does anyone know it? If noone knows, I wrote a program that enumerates USB devices and displays their Product and Manufacturer name, is anybody willing to test it? (you can run it in Avast! Sandbox if you prefer, it still works ^^)

Thanks in advance, and thanks for all the responses yet!

Oh by the way, using RawInput is out of question because of its Windows exclusive API, I thought about it once, but I want my game to be Windows/Linux/Mac compatible ;)

Edit2 : As marked in the tags, I'm coding in C++ (was also part of the original post)! I really don't understand what happened, since the original post title even changed =/ That was not my original question, the question was about the PS/2 / USB thing!

  • 6
    \$\begingroup\$ Windows? iOS? X11/GTK? SDL? What platform/API are you writing for? The answer is different for each. \$\endgroup\$ May 28, 2013 at 17:37
  • \$\begingroup\$ Very similar to this question: Detect two different mice at the same time \$\endgroup\$ May 28, 2013 at 21:13
  • \$\begingroup\$ As @SeanMiddleditch pointed out to me in an answer I deleted, there's a Windows API you can work with that overcomes the older problem of drivers stripping identifiers: msdn.microsoft.com/en-us/library/windows/desktop/… Give Sean credit if you find that useful. \$\endgroup\$ May 28, 2013 at 22:32
  • \$\begingroup\$ Wow well sorry, a lot of my original post was deleted, a whole lot! Well anyway, editing right now to fit better! \$\endgroup\$ May 29, 2013 at 11:10
  • \$\begingroup\$ Thanks to everyone by the way ^^ @John McDonald : Unfortunately, I could find some library for the mouse, mostly ManyMouse, which is cross platform, but absolutely no equivalent for keyboard! \$\endgroup\$ May 29, 2013 at 11:38

3 Answers 3


Assuming Windows, an example of RawInput is on CodeProject, written for C#.

The gist is that you must use the RawInput API to enumerate input devices such as all keyboard devices. For each device you can then get a handle to the individual input device. This handle is passed in to the RawInput message reading API and stored in RAWINPUTHEADER structure. You can compare these to determine which keyboard the message came from. You could then correlate this with enumerated mouse devices to make it so the "first mouse" matches the "first keyboard" (which might change every time the app is run or whenever the computer is rebooted or whenever devices are (a|de)ttached; I don't know).

Note to be careful as many modern gaming mouse are also technically keyboards. Pressing some extended buttons on such mice will generate a keyboard event. Likewise, some keyboards also identify as mice for various reasons. You'll want a way to detect/select which keyboards/mice to use for each player rather than just taking the first two enumerated devices, just to be sure.

Note also that XInput supports up to four players on gamepads directly and does not use Win32 input events, raw or otherwise.

SDL does not appear to support multiple keyboards in its public API, which is disappointing. I'm unsure what the story is on GFML, SFML, etc.

GTK seems to have a group field in GdkEventKey, which is maybe what you're looking for. The documentation is not clear, so I don't know. The low-level X11 API definitely supports it through the deviceid field of X11 input events. IF you're using low-level input on Windows you should probably do the same on Linux (if you structure code sanely it's not hard to port), though using a high-level GUI library like GTK is preferable in general.

OSX should in theory support it via the Quartz Event Services API but I am not familiar with coding low-level input for their platform. You might have to delve into the "deprecated" Carbon API, which I've heard supports it but I can't find any examples online right now.

I'm unsure what the USB support on iOS is like, though I suppose you probably aren't looking to hook up multiple (or any) keyboards to an iPad or anything. Android should have support, too, but again I have zero experience with its low-level input capabilities.

JavaScript/DOM does not support it at all nor are there are workarounds currently; the DOM events API simply doesn't expose device information for keyboards/mice. The only way to properly handle multiple players with separate input devices there is the new gamepad API.

  • \$\begingroup\$ Well, for GFML I have to check it out ;) For SFML he plans on adding this feature for 3.0, but... I just can't wait for it, it will take him a long time to make, however I use 2.0 for windowing, OpenGL interfacing and generic gamepads input. If my libusb code doesn't work I will look into GDK, I definitely would prefer a high level library for that kind of thing, too many little things to think about and I don't want to reinvent the wheel =/ For the devices and OSs I added some precision in my original post ;) \$\endgroup\$ May 29, 2013 at 11:24
  • \$\begingroup\$ About SDL too, they added it once, but gave up on it =/ Too bad! About the detect/select issue you're talking about, I was thinking of listening the devices and ask the user to generate any input on the device he wants to use ;) Anyway this feature seems to have many people wondering about how to do it, and yet many people seem to have gave up, really complex stuff here ! \$\endgroup\$ May 29, 2013 at 11:27
  • \$\begingroup\$ Sorry for the triple comment, is there an edit button for comments? I can see one right after criting the comments, but as soon as I refresh the page I can't :( Anyway, do you know of any reason why drivers are so "protective" in respect to the devices. Not exposing its unique device ID, product name, the port to which is plugged? Is there any security reason, because I can't see any! \$\endgroup\$ May 29, 2013 at 11:36
  • \$\begingroup\$ The drivers aren't generally all that protective, it's the higher-level APIs. They often don't bother with multiple devices because the desire for them is super small and it would just be more API to code, document, and maintain. Dig deep enough into the device APIs and all that info is there, but they're not APIs that are known or understood by a wide variety of people. Windows hides it all in the registry and small low-level APIs, Linux typically has its sysfs (iirc) interface, and I'm sure OSX/iOS have equivalents (unsure if Android uses Linux's sysfs or not). \$\endgroup\$ May 29, 2013 at 17:17
  • \$\begingroup\$ Yes accessing devices for "raw" input in unix is doable with the device path =/ Well about your other comment, the issue is that it's not a small kernel =/ Raw input APIs recognize keys differently, but my game engine will need to recognize it the same way, so there should be an additional abstraction layer which will standardize inputs for the engine, and that's both an issue in term of time "wasting" but also in speed for the game (though I do agree that things like UnrealEngine do real-time script interpretation, and not so well optmized, so it would not be too much of a hassle =/ ) \$\endgroup\$ May 30, 2013 at 7:30

I know you asked not to use RawInput, unfortunately on Windows, it will not be possible to use raw USB/HID for mice and keyboards. Windows has an exclusive file lock on mice and keyboards and will not allow you to read from the device directly. You will have to use RawInput or some other Windows specific API for mice and keyboards. I recommend you use LibUSB like you wanted to and simply use #pragmas to separate out the Windows specific code.


ifdef is your best code portability tool. Write your high level API with multiple mice in mind. Write your low level API in such a way so it uses RawInput on Windows and whatever you like in Linux and Mac. By the way, the fun part, you cannot obtain HID devices on Windows without using RawInput, so you must use it anyway :)


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .