All Projects β†’ reecelucas β†’ react-use-hotkeys

reecelucas / react-use-hotkeys

Licence: MIT license
React hook for creating simple keyboard shortcuts

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to react-use-hotkeys

Fre
πŸ‘» Tiny Footprint Concurrent UI library for Fiber.
Stars: ✭ 3,195 (+4217.57%)
Mutual labels:  hook, react-hooks
keys
⌨️ Keyboard Shortcuts for 'shiny'
Stars: ✭ 37 (-50%)
Mutual labels:  hotkeys, keyboard-shortcuts
Pyhooked
Pure Python hotkey hook, with thanks to pyHook and pyhk
Stars: ✭ 150 (+102.7%)
Mutual labels:  hook, hotkeys
formalizer
React hooks based form validation made for humans.
Stars: ✭ 12 (-83.78%)
Mutual labels:  hook, react-hooks
react-hook-videojs
Easy React integration of Video.js using hooks.
Stars: ✭ 37 (-50%)
Mutual labels:  hook, react-hooks
window-scroll-position
React hook for Window scroll position
Stars: ✭ 81 (+9.46%)
Mutual labels:  hook, react-hooks
react-use-countdown
React hook for countdown state.
Stars: ✭ 19 (-74.32%)
Mutual labels:  hook, react-hooks
Hotkeys
➷ A robust Javascript library for capturing keyboard input. It has no dependencies.
Stars: ✭ 5,165 (+6879.73%)
Mutual labels:  hotkeys, keyboard-shortcuts
Chordly
Chordly is a javascript library that may be used to detect and act upon key sequences entered by a user.
Stars: ✭ 14 (-81.08%)
Mutual labels:  hotkeys, keyboard-shortcuts
usehooks-ts
React hook library, ready to use, written in Typescript.
Stars: ✭ 2,873 (+3782.43%)
Mutual labels:  hook, react-hooks
useAudioPlayer
Custom React hook & context for controlling browser audio
Stars: ✭ 176 (+137.84%)
Mutual labels:  hook, react-hooks
ReSift
A state management library for data fetches in React
Stars: ✭ 39 (-47.3%)
Mutual labels:  hook, react-hooks
react-hook-layout
Layouts in React.
Stars: ✭ 16 (-78.38%)
Mutual labels:  hook, react-hooks
use-dencrypt-effect
βš› A custom React hook generating crypting text effect.
Stars: ✭ 39 (-47.3%)
Mutual labels:  hook, react-hooks
Cropper
Point and shoot screen captures
Stars: ✭ 100 (+35.14%)
Mutual labels:  hotkeys, keyboard-shortcuts
use-route-as-state
Use React Router route and query string as component state
Stars: ✭ 37 (-50%)
Mutual labels:  hook, react-hooks
react-hotkey-tooltip
A global Hotkey provider with built in tooltip for React
Stars: ✭ 34 (-54.05%)
Mutual labels:  hotkeys, keyboard-shortcuts
web
React hooks done right, for browser and SSR.
Stars: ✭ 940 (+1170.27%)
Mutual labels:  hook, react-hooks
react-class-hooks
React Hooks implementation for Class Components. Support React >= 15.3.2
Stars: ✭ 63 (-14.86%)
Mutual labels:  react-hooks, react-use
react-ui-hooks
🧩Simple repository of React hooks for building UI components
Stars: ✭ 20 (-72.97%)
Mutual labels:  hook, react-hooks

react-use-hotkeys

React hook for creating simple keyboard shortcuts.

Coverage Status Build Status npm bundle size (scoped) npm (scoped) GitHub

Installation

npm install @reecelucas/react-use-hotkeys

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');
});

useHotkeys('w " " d', () => {
  // space key in sequence (`w ' ' d` also works)
  console.log('Some action');
});

// Multiple key combinations mapped to the same callback
useHotkeys(['Control+z', 'Meta+z'], () => {
  console.log('Some action');
});

useHotkeys(['a', 'Meta+z', 'w s d'], () => {
  console.log('Some action');
})

The following patterns are not supported:

// 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!");
});

You can pass AddEventListenerOptions if you need to listen for keydown events in the capturing phase:

useHotkeys('Escape', () => {
  console.log('Some action');
}, true);

useHotkeys('Escape', () => {
  console.log('Some action');
}, { capture: true });

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:

useHotkeys('*', event => {
  console.log("I will run on every keydown");

  if (customKeyLogic(event)) {
    console.log("some action");
  }
});

Call Signature

useHotkeys(
  hotkeys: string | string[],
  callback: (event: KeyboardEvent) => void,
  eventListenerOptions?: boolean | AddEventListenerOptions
) => void;

Tests

Tests use Jest and react-testing-library.

git clone [email protected]:reecelucas/react-use-hotkeys.git
cd react-use-hotkeys
yarn
yarn test

LICENSE

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].