Small and lightweight, I like:
import tinykeys from "tinykeys"
tinykeys(window, {
"Shift+D": () => {
alert("The 'Shift' and 'd' keys were pressed at the same time")
},
"y e e t": () => {
alert("The keys 'y', 'e', 'e', and 't' were pressed in order")
},
"$mod+KeyD": () => {
alert("Either 'Control+d' or 'Meta+d' were pressed")
},
});
When calling tinykeys
it returns an unsubscribe function. Handy when developing components:
import { useEffect } from "react"
import tinykeys from "tinykeys"
function useKeyboardShortcuts() {
useEffect(() => {
let unsubscribe = tinykeys(window, {
// ...
});
return () => {
unsubscribe(); // make use of the returned unsubscribe() function.
}
})
}
Installation per NPM/Yarn
npm install tinykeys