Some keyboard "chords" (combinations of keys pressed simultaneously) will not register properly in the browser (Chrome and Firefox tested). For example, with the code below, try this:
1) press the "e" key (it will log "key 69")
2) while holding down "e", press "]" (it will log "key 221")
3) while still holding down "e" and "]", press "i" (this fails to log!)
4) however, if you let up on "i" and press "o" instead, it will successfully log "key 79".
document.onkeydown = function(event){
var key = event.keyCode;
console.log("key", key);
};
Can anyone explain what is going on here, and if there is any sort of workaround? For context, I'm developing a QWERTY-based music app, and I'd like to be able to play all my chords.
I'm aware that keyCode
is deprecated, perhaps this behavior one of the reasons?
What is the right approach for this problem?
Demo here: https://jsfiddle.net/nt0ad2ap/
I'm 99.9% sure that's depends on the keyboard's hardware itself. I tried doing e + ] + i on a keyboard connected to a laptop and it does not work but when I press e + ] on that keyboard and then press i on the laptop then it works. Keyboard are not usually built to register that many keys at once (aside from shift, ctrl and alt) because it is not normally needed. There are special keyboard built mostly for gaming which can register more keys at once. Try googling "anti-ghosting keyboards". Here is a question on Quora about the topic. I have a keyboard which suppose to register 13 keys pressed at once at home. I'll check if the code works with that keyboard in few hours when I get home.
Works fine on a better keyboard. It's a hardware problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With