本文介绍了在JavaScript / Web浏览器中捕获2个或更多键盘的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些执行简单键盘事件捕获的测试应用程序。

I am writing some test applications that perform simple keyboard event capture.

我对这个基本方法没有任何问题。

I have no problem with this basic approach.

但现在我需要更好的应用程序体验,我想连接至少两个或更多USB或PS / 2键盘,区分键盘是关键事件的来源,因此能够在本地使用带有JavaScript(和HTML5)的Web浏览器,就好像它是一个游戏控制台,同时为多个玩家提供多个控制器。

我知道即使在Windows或Linux下,正常的行为是让几个键盘触发相同的事件。但是,我也知道很有可能知道特定键盘硬件设备触发了该事件。

I know that even under Windows or Linux, the normal behavior is to make several keyboards fire the same events. However, I also know that it would be perfectly possible to know what particular keyboard hardware device fired that event.

它是否可能或是JavaScript的限制?未来可能会支持支持相同基本类型的多个输入设备的区别,是否有一些替代方案,至少使用HTML5,或者它几乎失去了希望,在Web浏览器的上下文中谈论,JavaScript和HTML5?

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

推荐答案

不,在当前的浏览器环境中,您不能支持特定类型的多个不同输入设备。我也不知道任何即将推出的规格可以实现这样的功能。通常,假设浏览器一次只有一个用户。我不认为会支持这样的事情。

No, with current browser environments you can not support multiple distinct input devices of a particular type. I am also not aware of any upcoming specs that would enable something like this. Generally it is assumed that a browser has only one user at at time. I don't think anything like this will be supported.

那就是说,有一个支持目前正在开发中。非开发版浏览器不支持它,但Firefox和Chrome都在开发它。如果您对此技术感兴趣,已经有一个名为的有趣库。

That said, there is a spec for gamepad support that is currently being developed. It is not supported by non-dev builds of browsers yet but both Firefox and Chrome are working on it. There is already an interesting library called gamepad.js if you are interested in this technology.

目前,为了支持多个玩家,您可能需要执行大多数游戏所做的操作来支持多个基于键盘的玩家,将每个玩家映射到不同的按键群。

For now, to support multiple players you will probably have to do what most games do to support multiple keyboard based players, map each player to a different cluster of keys.

已经变得更好,因为这个答案,Firefox和Chrome现在支持回溯几个版本,Opera也支持它。但并非所有浏览器都这样做,Safari和IE都没有。微软的新浏览器Edge看起来它将支持Gamepad API,但可能需要很长时间才能让大多数IE用户迁移到Edge或其他浏览器。

Support for the Gamepad API has gotten better since this answer, both Firefox and Chrome have support going back several versions now, and Opera supports it too. But not all browsers do, Safari and IE do not. Microsoft's new browser Edge looks like it will have support for the Gamepad API but it will probably take a long time to get most of the IE users to migrate to Edge or another browser.

这篇关于在JavaScript / Web浏览器中捕获2个或更多键盘的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 21:01