In javascript ,onkeyup event, when I press shfit + key , then release the shift before the key, I don't get the event.key for the key , instead I get "shift".
Is there a way to get the event.key for the key ?
On keyup event, when shift + key released , shift before key , event.key not reflects key
61 views Asked by Yaakov Whise At
2
There are 2 answers
0
On
Remember pressed keys in a set (this prevents registering repeating keydowns):
const pressedKeys = new Set;
document.addEventListener('keydown', e => pressedKeys.add(e.key));
document.addEventListener('keyup', e => {
pressedKeys.delete(e.key);
e.key === 'Shift' && pressedKeys.size && console.log('Still pressed:', ...pressedKeys);
pressedKeys.clear();
});
Key up triggers the moment a key is released. It's impossible to release two keys at the same time but it is possible to press them at the same time: