I’m trying to add a hotkey functionality to my Google Chrome extension. If the user holds Alt, a class is added to body.
The problem is that if user changes browser tab, window, or clicks in dev tools area the class is not removed. How I can make sure the class gets removed when the current tab loses “focus”?
document.addEventListener('keydown', keydown);
var showing = false;
function keydown(evt) {
if (evt.altKey && showing == false) {
document.body.classList.add('key-pressed');
}
}
document.addEventListener('keyup', keyup);
function keyup(evt) {
if (evt.key === 'Alt') {
showing = false;
document.body.classList.remove('op-key-pressed');
}
}