Skip to content

Instantly share code, notes, and snippets.

@w3collective
Last active February 28, 2022 00:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save w3collective/9ef9c792aaf9d8cfafb37de6582ccd66 to your computer and use it in GitHub Desktop.
How to create keyboard shortcuts for web apps with JavaScript
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;
}
@w3collective
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment