I have a need to detect the keystroke the user is pressing, and if it is a combination of keys (such as command + shift + p),), I will perform a certain operation.
but encountered a problem, that is, the keyup event of the composite key did not trigger, but only triggered the keydown.
Code:
const pressingKeyCode = {};
const COMMAND = 91;
const SHIFT = 16;
const P = 80;
window.addEventListener("keydown", e => {
console.log("keydown", e.keyCode);
pressingKeyCode[e.keyCode] = true;
if (
pressingKeyCode[COMMAND] &&
pressingKeyCode[SHIFT] &&
pressingKeyCode[P]
) {
console.log("");
}
});
function handleKeyUp(e) {
console.log("keyup", e.keyCode);
pressingKeyCode[e.keyCode] = false;
}
window.addEventListener("keyup", handleKeyUp);
there is no problem with a single keystroke
reproducible demo address: https://codepen.io/zhipenglu/.