JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5298
  • Score
    100M100P100Q142449F
  • License MIT

React hook to create keyboard shortcuts

Package Exports

  • @reecelucas/react-use-hotkeys

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@reecelucas/react-use-hotkeys) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

react-use-hotkeys

React hook for creating keyboard shortcuts.

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
useHotkeys('Escape', () => {
  console.log('some action');
});

useHotkeys('F7', () => {
  console.log('some action');
});

// Modifier combinations
useHotkeys('Meta+Shift+z', () => {
  console.log('some action');
});

// Key sequences
useHotkeys('w s d', () => {
  console.log('some action');
});

The following patterns are not supported (yet).

// Modifier keys in sequences
useHotkeys('Control i d', () => {
  console.log("I won't run!");
});

// Modifier combinations in sequences
useHotkeys('Control+z i d', () => {
  console.log("I won't run!");
});

// Multiple combinations mapped to the same callback
useHotkeys(['Control+z', 'Meta+z'], () => {
  console.log("I won't run!");
});

Call Signature

useHotkeys(hotkeys: string, callback: (event: KeyboardEvent) => void);

Tests

Tests use Jest and react-testing-library.

git clone git@github.com:reecelucas/react-use-hotkeys.git
cd react-use-hotkeys
yarn
yarn test

LICENSE

MIT