react-use-hotkeys
React hook for creating simple keyboard shortcuts.
Installation
npm install @reecelucas/react-use-hotkeys
This package has a single dependency, a tiny shim called shim-keyboard-event-key
that normalises the non-standard KeyBoardEvent.key
values implemented in Edge and IE.
Example Usage
All hotkey combinations must use valid KeyBoardEvent
"key"
values. A full list can be found on MDN and Wes Bos has created a great interactive lookup.
// Single keys; ; // Modifier combinations; // Key sequences; ;
The following patterns are not supported (yet):
// Modifier keys in sequences; // Modifier combinations in sequences; // Multiple combinations mapped to the same callback;
If you find a use case where the API is too restrictive you can use the escape hatch to perform whatever custom logic you need:
;
Call Signature
useHotkeyshotkeys: string, callback:void;
Tests
Tests use Jest and react-testing-library.
git clone git@github.com:reecelucas/react-use-hotkeys.gitcd react-use-hotkeysyarnyarn test