Package
This is a very simple package that allows you to register hotkeys to run functions easily!
pnpm add @ekwoka/hotkeys
hotkeys({
'ctrl+c': () => console.log('copying!!!'),
'ctrl+k': openSearchBar,
'cmd+p': openCommandPalette,
'cmd+ctrl+alt+shift+u': activateSuperUser,
});
The string accepts ctrl
, alt
, cmd
, and shift
as modifiers, or any combination thereof.
The function also returns a function to unregister the hotkeys.
const unregister = hotkeys({
'ctrl+c': () => console.log('copying!!!'),
'ctrl+x': () => unregister(),
});
This allows it to be easily used inside useEffect
for Preact and React to allow easy reactive hotkeys, or cleaning up components registered hotkeys when they are unloaded.
const [counter, setCounter] = useState(0);
useEffect(
() =>
hotkeys({
'ctrl+y': () => setCounter(counter + 1),
}),
[counter]
);