Last active
February 28, 2022 00:49
Star
You must be signed in to star a gist
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/