Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some "chords" do not work with onkeydown (JavaScript)

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/

like image 338
drenl Avatar asked Oct 29 '22 21:10

drenl


1 Answers

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.

UPDATE:

Works fine on a better keyboard. It's a hardware problem.

like image 58
Dmitry Avatar answered Nov 09 '22 11:11

Dmitry