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?