4.5.0 • Published 3 months ago

react-hotkeys-hook v4.5.0

Weekly downloads
47,333
License
MIT
Repository
github
Last release
3 months ago

react-hotkeys-hook

React hook for using keyboard shortcuts in components. This is a hook version for the hotkeys package.

Documentation and live example

https://johannesklauss.github.io/react-hotkeys-hook/

Installation

npm install react-hotkeys-hook

or

yarn add react-hotkeys-hook

Make sure that you have at least version 16.8 of react and react-dom installed, or otherwise hooks won't work for you.

Usage

export const ExampleComponent = () => {
  const [count, setCount] = useState(0);
  useHotkeys('ctrl+k', () => setCount(prevCount => prevCount + 1));
    
  return (
    <p>
      Pressed {count} times.
    </p>
  );
};

The hook takes care of all the binding and unbinding for you. As soon as the component mounts into the DOM, the key stroke will be listened to. When the component unmounts it will stop listening.

Call Signature

useHotkeys(keys: string, callback: (event: KeyboardEvent, handler: HotkeysEvent) => void, options: Options = {}, deps: any[] = [])

The callback function takes the exact parameters as the callback function in the hotkeys package. See hotkeys documentation for more info or look into the typings file.

Parameters

  • keys: string: Here you can set the key strokes you want the hook to listen to. You can use single or multiple keys, modifier combination, etc. See this section on the hotkeys documentation for more info.
  • callback: (event: KeyboardEvent, handler: HotkeysEvent) => void: Gets executed when the defined keystroke gets hit by the user. Important: Since version 1.5.0 this callback gets memoised inside the hook. So you don't have to do this anymore by yourself.
  • options: Options = {}
    • filter: (event: KeyboardEvent): boolean is used to enable hotkeys inside input elements. Check out hotkeys docs for usage. Due to constraints with the base library, filter is a global setting. As a result, it is currently not possible to have different filters for separate calls of useHotkey.
      • enableOnTags: string[] is used to enable listening to hotkeys in form fields. Available values are INPUT, TEXTAREA and SELECT. IMPORTANT! When you provide a custom filter implementation function this parameter will be ignored. Similar to filter, setting this option will enable it globally.
    • splitKey: string is used to change the splitting character inside the keys argument. Default is +, but if you want to listen to the + character, you can set splitKey to i.e. - and listen for ctrl-+
    • keyup: boolean Determine if you want to listen on the keyup event
    • keydown: boolean Determine if want to listen on the keydown event
  • deps: any[] = []: The dependency array that gets appended to the memoisation of the callback. Here you define the inner dependencies of your callback. If for example your callback actions depend on a referentially unstable value or a value that will change over time, you should add this value to your deps array. Since most of the time your callback won't depend on any unstable callbacks or changing values over time you can leave this value alone since it will be set to an empty array by default. See the Memoisation section to learn more and see an example where you have to set this array.

useIsHotkeyPressed hook

The useIsHotkeyPressed hook just returns the hotkeys.isPressed function and works exactly the same.

const isPressed = useIsHotkeyPressed();

isPressed('return'); // Returns true if Return key is pressed down.

Found an issue or have a feature request?

Open up an issue or pull request and participate.

Local Development

Checkout this repo, run yarn or npm i and then run the docz:dev script. You can use the docs/useHotkeys.mdx to test the behavior of the hook. It directly imports the src/index.ts file and transpiles it automatically. So you don't have to worry about. For more info on .mdx files, check out the docz documentation: https://www.docz.site/docs/writing-mdx

Authors

  • Johannes Klauss

MIT License.


@codecademy/gamut-labs@uponco/admin-uiosmo-live-annotations-toolbar-npm-testosmo-live-annotations-toolbar-testlive-toolbar-test-internallive-toolbar-test1power-logs-miner@mehdi9795/componentsawwsm-widgetfeathery-reactoperand-jshahnbee-testlowcode-flowhahnbee-test-client@beepsoft/ocho@beepsoft/ocho-2@beepsoft/ocho-3@beepsoft/ocho-6@infinitebrahmanuniverse/nolb-react-hodemotestpublish@knapsack/app-uiart47mui-react-chat@everything-registry/sub-chunk-2560medusa-admin-ui-faeditmode-reactekbelabnext_editorelabnext_reportenjoys-discoengioscope@obigtech/ui@schickling-tmp/react-notion-x@sgftech/admin-ui@shaderfrog/editor@sellerartifact-medusajs/admin-ui@semic/layout@labelu/audio-annotator-react@labelu/video-annotator-react@labelu/audio-react@labelu/components@labelu/components-react@labelu/image-annotator-react@labelu/video-react@lobehub/chat@nocobase/clientnotion-forge@novapo/medusajs-admin-uinpm-all-packages@lrocher/medusa-admin-ui@openchatai/command@openchatai/search@openworkshop/maker-hub@optidist/admin-ui@etherealengine/editormetasnippets@hokoup/react@hopeness.com/react-notion-x@ecs-client/oss@ecs-client/oss-test@ecs-client/uimohimmatech-ui@ezcax/editor@hirvesh/react-notion-x@dyi919/tldraw@helixml/chat-widget@lambdacurry/gatsby-theme-medusa-admin@ladle/reactuse-theme-editor@modfy/tldraw@monokle/components@kozmoai/rag-ai@mongodb-js/compass-components@nerdjs/kronos-kit@nerdjs/nerd-ui@libreforge/libreforge-designer@netlambda/admin-uiva-react-notion-x@nelomobile/medusa-admin-ui@nextunicorn/react-notion-xuniversal-command-palette@moosty/lisk-explorer@krapi0314/tldraw@q-dev/q-ui-kit@react-jopau/components@simonghales/react-three-scene-editor@renderui/hooks@stacks/ui@sroussey/ngraph@redlotus/ui@paultalarian/react-notion-x@payglo/admin-ui@pinpt/uic.nextdpcl@pinpt/block-editor@nubebytes/ui-react@operandinc/react@plugandwork/ecm@structure.rest/editor@sumbat/react-diagramax
5.0.0-1

3 months ago

4.5.0

3 months ago

4.4.4

3 months ago

5.0.0-0

4 months ago

4.4.3

4 months ago

4.4.2

4 months ago

4.4.1

10 months ago

4.3.6-4

1 year ago

4.4.0

1 year ago

4.3.6-0

1 year ago

4.3.6-1

1 year ago

4.3.6-2

1 year ago

4.3.6-3

1 year ago

4.3.4

1 year ago

4.3.3

1 year ago

4.3.6

1 year ago

4.3.5

1 year ago

4.3.8

1 year ago

4.3.7

1 year ago

4.1.0-0

1 year ago

4.1.0-1

1 year ago

4.0.5

1 year ago

4.0.4

1 year ago

4.0.7

1 year ago

4.0.6

1 year ago

4.0.1

1 year ago

4.0.0

1 year ago

4.2.1

1 year ago

4.0.3

1 year ago

4.2.0

1 year ago

4.0.2

1 year ago

4.0.8

1 year ago

4.0.4-0

1 year ago

4.0.4-3

1 year ago

4.0.0-7

1 year ago

4.0.4-4

1 year ago

4.0.4-1

1 year ago

4.0.4-2

1 year ago

4.0.0-6

1 year ago

4.0.4-5

1 year ago

4.3.2

1 year ago

4.3.1

1 year ago

4.1.0

1 year ago

4.3.0

1 year ago

4.1.1

1 year ago

4.0.0-0

2 years ago

3.4.7

2 years ago

4.0.0-3

2 years ago

4.0.0-4

2 years ago

4.0.0-1

2 years ago

4.0.0-2

2 years ago

4.0.0-5

2 years ago

3.4.6

2 years ago

3.4.5

2 years ago

3.4.4

3 years ago

3.4.3

3 years ago

3.4.2

3 years ago

3.4.1

3 years ago

3.4.0

3 years ago

3.3.3

3 years ago

3.3.2

3 years ago

3.3.1

3 years ago

3.3.0

3 years ago

3.2.1

3 years ago

3.2.0

3 years ago

3.1.0

3 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

2.4.1

3 years ago

3.0.0

3 years ago

2.4.0

3 years ago

2.3.1

4 years ago

2.3.0

4 years ago

2.3.0-3

4 years ago

2.3.0-2

4 years ago

2.3.0-1

4 years ago

2.3.0-0

4 years ago

2.2.2

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.4

4 years ago

2.1.3

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.4

4 years ago

1.5.3

5 years ago

1.5.2

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.4

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago