Last active
February 28, 2022 00:49
-
-
Save w3collective/9ef9c792aaf9d8cfafb37de6582ccd66 to your computer and use it in GitHub Desktop.
How to create keyboard shortcuts for web apps with JavaScript
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
let keyDown = { | |
'c': false, | |
'p': false, | |
's': false, | |
'Meta': false, | |
'Control': false | |
}; | |
document.addEventListener('keydown', handleKeydown); | |
function handleKeydown(e) { | |
e.preventDefault(); | |
keyDown[e.key] = true; | |
if (keyDown['c'] && (keyDown['Control'] || keyDown['Meta'])) { | |
console.log('Shortcut Activated'); | |
}; | |
} | |
document.addEventListener('keyup', handleKeyup); | |
function handleKeyup(e) { | |
keyDown[e.key] = false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source => https://w3collective.com/keyboard-shortcuts-javascript/