When holding a single arrow key down the function works correctly.
However, the problem occurs when I then hold a second arrow key and release the second key the first held key is no longer detected.
A simplified version of my functions are as follows:
document.body.onkeyup = function(e){
if ([37,38,39,40].indexOf(e.keyCode) > -1){
var key_pressed = e.keyCode || e.which;
console.log(key_pressed + " is UP")
}
if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
e.preventDefault();
}
};
document.body.onkeydown = function(e){
if ([37,38,39,40].indexOf(e.keyCode) > -1){
var key_pressed = e.keyCode || e.which;
console.log(key_pressed + " is DOWN")
}
if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
e.preventDefault();
}
};
So, if I hold the UP arrow key, it says 38 is DOWN
repeatedly whileI am holding the key.
Then if I press the RIGHT arrow key it says 39 is DOWN
repeatedly while I am holding both keys.
Then if I let go of the RIGHT arrow key it says 39 is UP
.
My expectation would be for it to then continue to say 38 is DOWN
repeatedly again since I am still holding the UP arrow. It does not, however do this.
Why is this happening? What should I do to fix this so that my arrow key is still detected as being held down?