使用javascript读取多个同时键盘输入 [英] Reading multiple simultaneous keyboard inputs using javascript

查看:167
本文介绍了使用javascript读取多个同时键盘输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直注意到JavaScript中的键盘输入有些奇怪的行为。我可能会遗漏一些非常明显的东西,但有哪些规则可以同时按下哪些键?

I've been noticing some odd behaviour with keyboard input in JavaScript. I may be missing something really obvious here, but are there some kind of rules regarding which keys are allowed to be pressed simultaneously?

我正在使用布尔变量来保持状态对于以下四个键中的每一个,这允许许多同时按键(硬件允许):

I'm using boolean variables to hold state for each of four keys as follows, this allows for many simultaneous key presses (hardware permitting):

var up = false, left = false, right = false, space = false;

function keydown(e) {
    if (e.keyCode == 32)
        space = true;
    if (e.keyCode == 38)
        up = true;
    if (e.keyCode == 37)
        left = true;
    if (e.keyCode == 39)
        right = true;
}

function keyup(e) {
    if (e.keyCode == 32)
        space = false;
    if (e.keyCode == 38)
        up = false;
    if (e.keyCode == 37)
        left = false;
    if (e.keyCode == 39)
        right = false;
}

在两台机器上我试过以下jsfiddle让你按空格键,例如,同时向上和向右,但不是空间,向上和向左。在这两台机器上,它在Chrome,FF和IE中也是如此。在第三台机器上,它可以完美地工作,我可以同时按住所有4个键。

On two machines I've tried the following jsfiddle allows you to press space, up and right simultaneously, but not space, up and left, for example. On those two machines it's doing the same in Chrome, FF and IE. On a third machine it works flawlessly and I can hold all 4 keys simultaneously.

现在可能这与硬件有关,但我的主要问题是为什么存在差异操作左右键?它似乎不一致,我确信它的原因是正确的。

Now presumably this is hardware related, but my main question is why there is a difference in the operation of the left and the right keys? It seems inconsistent and I'm sure there is a valid reason why it's the way it is.

http://jsfiddle.net/SYs5b/

(您必须在结果窗格中单击以获取事件触发)

(You have to click within the results pane to get the events firing)

推荐答案

为了省钱,键盘制造商经常在同一总线上放许多键。这可以防止同时按下键盘的同一区域中的多个键。有时它甚至可以防止整个键盘上的两个以上的按键被同时按下。 shift,ctrl和alt键通常不在此限制范围内,因此您可以按住shift并同时按下其他2个键,它仍然可以正常工作。

In order to save money, keyboard manufacturers often put many keys on the same bus. This prevents multiple keys in the same region of the keyboard from being pressed simultaneously. Sometimes it even prevents more than 2 keys at all from across the whole keyboard being pressed at once. Often the shift, ctrl, and alt keys are not within this limitation, so you can hold shift and press 2 other keys at once and it will still work fine.

Even高端游戏键盘通常具有类似的硬件限制,但上限要高得多,因此在正常游戏过程中不太可能达到。

Even high-end gaming keyboards often have a similar hardware limitation, although the cap is much higher so that it is unlikely to be reached during the normal course of gaming.

这是也就是所谓的重影,当你按下的键似乎没有注册时。

This is also known as "ghosting", when keys you press seem not to register.

这篇关于使用javascript读取多个同时键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆