Capturing events of 2 or more keyboards in a JavaScript / web browser

I am writing several test applications that perform simple keyboard grabbing.

I have no problem with this basic approach.

But now I need a better experience with my application, and I want to connect at least two or more USB or PS / 2 keyboards, distinguishing which keyboard is the source of key events, and therefore the ability to use a web browser locally with JavaScript (and HTML5) as if it were a game console with several controllers for several players simultaneously.

I know that even on Windows or Linux, the normal behavior is to force multiple keyboards to fire the same events. However, I also know that it would be entirely possible to find out which particular keyboard hardware device generated this event.

Is this possible or is this a limitation in JavaScript? There will probably be support in the future to support the distinction of multiple input devices of the same basic type, is there some kind of alternative, at least with HTML5, or will it be an almost lost hope, speaking in the context of a web browser, JavaScript and HTML5?

+4
source share
1 answer

No, with current browser environments you cannot support several different input devices of a certain type. I am also not aware of any upcoming specifications that would allow something like this. It is generally assumed that a browser has only one user at a time. I do not think something like this will be supported.

However, there is a specification for the gamepad support that is currently under development. It is not supported by non-dev browser builders, but Firefox and Chrome are working on it. There is an interesting library called gamepad.js if you are interested in this technology.

Currently, to support multiple players, you probably have to do what most games do to support multiple players based on the keyboard, map each player to a different key cluster.

Update 2015-05-04

Gamepad API support has improved since the response, and Firefox and Chrome now support multiple versions, and Opera supports it too. But not all browsers, Safari and IE do not. Microsoft's new Edge browser looks like it will support the Gamepad API, but it will probably take a long time to get most IE users to switch to Edge or another browser.

+3
source

All Articles