Seems like others have ran into this issue outside of the browser. I'm trying to figure out how to deal with it in my javascript code.
When I hold command (on mac) and press and let go of another key, (such as if I were to enter command-c to copy some text), the keyup event does not fire unless I let go of command first.
This affects me because my web app responds to the alt-x key combination to toggle a menu, and this is important. I'm using keyup and keydown events to track the state of which keys are pressed at any given time, so that I can toggle the menu when the user enters alt-x (and not some other combination including alt-x). It works great until I enter command-[some key] and then my program thinks I'm entering [some key]-alt-x when I enter alt-x to toggle the menu.
Does anyone have any ideas for how to deal with this situation?
I made a code pen to illustrate the problem.
const pressed_keys = {};
const keydown_handler = e => {
pressed_keys[e.code] = true;
if (has_entered_alt_x(e)) {
document.querySelector('div').classList.toggle('blue');
}
};
const keyup_handler = e => pressed_keys[e.code] = false;
const has_entered_alt_x = e => {
if (!(e.altKey && e.code == 'KeyX')) return false;
for (const key in pressed_keys) {
if (Object.hasOwnProperty.call(pressed_keys, key)) {
if (!['AltLeft', 'AltRight', 'KeyX', 'CapsLock'].includes(key) &&
pressed_keys[key]) {
return false;
}
}
}
return true;
}
document.addEventListener('DOMContentLoaded', () => {
document.addEventListener('keydown', keydown_handler);
document.addEventListener('keyup', keyup_handler);
});
div {
width: 40px;
height: 40px;
background-color: red;
}
.blue {
background-color: blue;
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"/>
</head>
<body>
<div></div>
</body>
</html>