Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sandiiarov/use-hotkeys

➷ Hotkeys React Hook
https://github.com/sandiiarov/use-hotkeys

Last synced: about 1 month ago
JSON representation

➷ Hotkeys React Hook

Awesome Lists containing this project

README

        

# Use Hotkeys

![npm](https://img.shields.io/npm/dt/use-hotkeys.svg)
![npm](https://img.shields.io/npm/v/use-hotkeys.svg)
![NpmLicense](https://img.shields.io/npm/l/use-hotkeys.svg)

React wrapper around [Hotkeys.js](https://github.com/jaywcjlove/hotkeys).

```shell
╭┈┈╮ ╭┈┈╮ ╭┈┈╮
┆ ├┈┈..┈┈┈┈┈.┆ └┈╮┆ ├┈┈..┈┈┈┈┈..┈┈.┈┈..┈┈┈┈┈.
┆ ┆┆ □ ┆┆ ┈┤┆ < ┆ -__┘┆ ┆ ┆┆__ ┈┈┤
╰┈┈┴┈┈╯╰┈┈┈┈┈╯╰┈┈┈┈╯╰┈┈┴┈┈╯╰┈┈┈┈┈╯╰┈┈┈ ┆╰┈┈┈┈┈╯
╰┈┈┈┈┈╯
```

**Use Hotkeys** - React hook that listen to keyboard events, defining and dispatching keyboard shortcuts.

Read about [Hooks](https://reactjs.org/docs/hooks-intro.html) feature.

## Installation

> Note: React 16.8+ is required for Hooks.

### With npm

```sh
npm i use-hotkeys
```

### Or with yarn

```sh
yarn add use-hotkeys
```

## Usage

[![Edit 1llx4n8q4](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/1llx4n8q4?fontsize=14)

```jsx
import useHotkeys from 'use-hotkeys';
```

```jsx
const Counter = () => {
const [count, setCount] = React.useState(0);

useHotkeys(
(key, event, handle) => {
switch (key) {
case 'up':
return setCount(count + 1);
case 'down':
return setCount(count - 1);
default:
return setCount(count);
}
},
['up', 'down'],
[count]
);

return

{count}
;
};
```