Skip to content

Instantly share code, notes, and snippets.

@av01d
Created March 29, 2024 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save av01d/38ac8b6711b74cb389d602a2686f6236 to your computer and use it in GitHub Desktop.
Save av01d/38ac8b6711b74cb389d602a2686f6236 to your computer and use it in GitHub Desktop.
Javascript keypress handler
const commands = {
'ctrl+z' => History.undo,
'ctrl+y' => History.redo,
'escape': () => { alert('esc'); }
};
$(window).on('keydown', event => {
let label = '';
if (event.ctrlKey) { label += 'ctrl+'; }
if (event.altKey) { label += 'alt+'; }
if (event.shiftKey) { label += 'shift+'; }
label += event.key.toLowerCase();
console.log(label);
commands[label]?.();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment