A very simple utility to listen for keyboard shortcuts
Quickstart
1. Install
npm install shortcut-listener
2. Listen for shortcuts
import { ShortcutListener } from "shortcut-listener";
const shortcuts = new ShortcutListener({
root: document.body, // The element to attach the keyboard events to
});
shortcuts.on("shortcutdown", ({ shortcut, keys, matches }) => {
// Check against a match function. This checks your shortcut against aliases
if (matches("cmd+option+j")) {
console.log("pressed META + ALT + J");
}
// Check against a standardized shortcut format
if (shortcut === "Control+Alt+j") {
console.log("pressed CTRL + ALT + J");
}
// Check against individual keys
if (keys.has("Control") && keys.has("Alt") && keys.has("j")) {
console.log("pressed CTRL + ALT + J");
}
});