3

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;
}

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)

Polynomial
  • 3,656
  • 23
  • 36
  • 1
    It most likely depends on the keyboard (or the keyboard driver). One keyboard might allow more keys being pressed simultaneously than another model/brand. Did you try using the same keyboard with all three machines? – Shaz Feb 07 '14 at 18:09
  • @Shaz You're right, it does depend on brand. But it also depends on **what** keys you press. SPACE + UP + RIGHT = 3 keys and works. SPACE + UP + LEFT = only 2 keys register. I'm wondering why the inconsistency, I suppose. – Polynomial Feb 07 '14 at 18:12
  • I think it's just the way different keyboards are wired. I remember this happening to me around 10 years ago. I'd have to use two different keyboards for two different games. – Shaz Feb 07 '14 at 18:20

1 Answers1

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 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.

CodeViking
  • 206
  • 1
  • 6
  • Thanks for the explanation. I still don't quite follow why SPACE + UP + RIGHT and SPACE + UP + LEFT exhibit completely different characteristics on those two keyboards though - I'd expect those sets of keys to work in the same way. Maybe that's why I'm not a keyboard manufacturer. – Polynomial Feb 07 '14 at 18:33
  • 1
    Interesting information. If anyone has further information or a link it would be very useful. The reason being that combining keys in js to improve productivity is useful but even with this answer I wouldn't know which key combinations would have a good chance of working. – Damien Golding Sep 06 '14 at 12:46