Created
October 29, 2019 07:25
-
-
Save ngsctt/c1520841530cd1add23a2cc2a9dd2156 to your computer and use it in GitHub Desktop.
Listen for simple hotkeys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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