Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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!

share|improve this question
4  
Windows? iOS? X11/GTK? SDL? What platform/API are you writing for? The answer is different for each. –  Sean Middleditch May 28 '13 at 17:37
    
Very similar to this question: Detect two different mice at the same time –  John McDonald May 28 '13 at 21:13
    
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. –  Patrick Hughes May 28 '13 at 22:32
    
Wow well sorry, a lot of my original post was deleted, a whole lot! Well anyway, editing right now to fit better! –  DevilBlackDeath May 29 '13 at 11:10
    
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! –  DevilBlackDeath May 29 '13 at 11:38
show 2 more comments

1 Answer

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.

share|improve this answer
    
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 ;) –  DevilBlackDeath May 29 '13 at 11:24
    
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 ! –  DevilBlackDeath May 29 '13 at 11:27
    
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! –  DevilBlackDeath May 29 '13 at 11:36
    
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). –  Sean Middleditch May 29 '13 at 17:17
    
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 =/ ) –  DevilBlackDeath May 30 '13 at 7:30
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.