Skip to content

Instantly share code, notes, and snippets.

@ngsctt
Created October 29, 2019 07:25
Show Gist options
  • Save ngsctt/c1520841530cd1add23a2cc2a9dd2156 to your computer and use it in GitHub Desktop.
Save ngsctt/c1520841530cd1add23a2cc2a9dd2156 to your computer and use it in GitHub Desktop.
Listen for simple hotkeys
const specialKey = navigator.userAgent.indexOf('Macintosh') != -1 ? "metaKey" : "ctrlKey";
const otherKeys = ['altGraphKey','altKey','ctrlKey','metaKey','shiftKey'].filter(k => k !== specialKey);
document.addEventListener('keydown', event => {
if (event.code === key && event[specialKey] === true && !otherKeys.some(k => event[k] === true)) {
// Do something
event.preventDefault();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment