Instead of timeouts, make non-related keypresses cancel shortcut sequences

This commit is contained in:
Henrik Giesel 2021-05-20 18:32:53 +02:00
parent ae19ed527d
commit ddeae60854

View file

@ -100,8 +100,6 @@ function check(
);
}
const shortcutTimeoutMs = 400;
function innerShortcut(
lastEvent: KeyboardEvent,
callback: (event: KeyboardEvent) => void,
@ -119,14 +117,12 @@ function innerShortcut(
if (check(event, optionalModifiers, nextKey)) {
innerShortcut(event, callback, optionalModifiers, ...restKeys);
clearTimeout(interval);
} else if (event.location === 0) {
// Any non-modifier key will cancel the shortcut sequence
document.removeEventListener("keydown", handler);
}
};
interval = setTimeout(
(): void => document.removeEventListener("keydown", handler),
shortcutTimeoutMs
);
document.addEventListener("keydown", handler, { once: true });
}
}