1

I am making a small browsergame within the Html5 canvas, using javascript. I am using the onKeyUp and onKeyDown events to get keyboard input:

function onKeyDown(evt) {
    if (evt.keyCode == 32) {
        spaceKey = true;
    }

    if (evt.keyCode == 38) {
        arrowUpKey = true;
    }

    if (evt.keyCode == 37) {
        arrowLeftKey = true;
    }

    if (evt.keyCode == 39) {
        arrowRightKey = true;
    }
}

function onKeyUp(evt) {
    if (evt.keyCode == 32) {
        spaceKey = false;
    }
    if (evt.keyCode == 38) {
        arrowUpKey = false;
    }

    if (evt.keyCode == 37) {
        arrowLeftKey = false;
    }

    if (evt.keyCode == 39) {
        arrowRightKey = false;
    }
}

I need the arrow keys (left, right and up) and the spacebar. The input is usually detected, but there is one specific case that's not working: If the arrows left and up are pressed (the player jumps and moves left at the same time) then for some odd reason any spacebar input is not detected.

In my code other than the shown events I am never assigning any new value to the variable 'spaceKey'. So i am assuming there is another reason that this specific combination of keys is not working or maybe there is just a better/safer way to handle user input?

oHoodie
  • 209
  • 4
  • 13
  • possible duplicate of [Detect multiple keys on single keypress event on jQuery](http://stackoverflow.com/questions/10655202/detect-multiple-keys-on-single-keypress-event-on-jquery) – Nikhil Aggarwal Jul 18 '15 at 18:20
  • I saw this post but my problem is a little bit different. Just found out it's working when I use the 'A' key instead of the spacebar. Can it be that the spacebar gets a different keycode when pressed in combination with other keys? (like 'a' is 'A' when shift is pressed at the same time...) – oHoodie Jul 18 '15 at 18:23

1 Answers1

1

Just to eliminate the keyboard as the actual issue, please check this link as to an explanation of why keyboards don't register some multi-key presses

http://www.microsoft.com/appliedsciences/antighostingexplained.mspx

You can us the demo to test the keyboard too

becoop
  • 36
  • 1
  • Very interesting and looks like the reason for my problem! Only thing I can do is use other keys that are 'valid' 3 key combinations. Thank you! – oHoodie Jul 19 '15 at 15:03