# react-keybinding-component

> A React keybinding component

Latest version **2.3.1** (published 2026-03-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-keybinding-component
pnpm add react-keybinding-component
yarn add react-keybinding-component
bun add react-keybinding-component
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.3.1 |
| Published | 2026-03-09 |
| First published | 2015-12-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 19.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Pierre de la Martinière |
| Maintainers | martpie |
| Keywords | react, keybinding, keybindings, key, binding, bindings, hotkey, keyboard, es6, mixin |

## Links

- npm: https://www.npmjs.com/package/react-keybinding-component
- Repository: https://github.com/martpie/react-keybinding-component
- Homepage: https://github.com/martpie/react-keybinding-component#readme
- Issues: https://github.com/martpie/react-keybinding-component/issues
- npm.io page: https://npm.io/package/react-keybinding-component

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.3.1 (latest) — 2026-03-09
- 2.3.0 — 2026-03-09
- 2.2.0 — 2024-05-16
- 2.0.2 — 2023-07-03
- 2.0.1 — 2023-07-03
- 2.0.0 — 2023-07-03
- 1.0.0 — 2018-11-22
- 0.5.2 — 2018-11-21
- 0.5.1 — 2017-07-30
- 0.5.0 — 2017-07-07
- 0.4.1 — 2015-12-22
- 0.4.0 — 2015-12-21
- 0.3.2 — 2015-12-05
- 0.3.1 — 2015-12-05
- 0.3.0 — 2015-12-04
- … 4 more at https://npm.io/package/react-keybinding-component/versions

## README

# react-keybinding-component
A React keybinding component, usable with es6, no mixin

### Description

react-keybinding-component is a component that will help you set up keybindings in your app. For the moment, only simple key events are supported (one key only, `keyup`, `keydown`). It's just a less dirty way of using key events instead of having care of them with `componentDidMount()` in multiple components.

### Examples

Import it in your project:
``` javascript
import KeyBinding from 'react-keybinding-component';
```

Get the eventKey keyCode and log it
``` JSX
<KeyBinding onKey={ (e) => { console.log(e.keyCode) } } />
```

Get the eventKey keyCode and log it on `keyUp` on the window element
``` JSX
<KeyBinding onKey={ (e) => { console.log(e.keyCode) } } type='keyup' target={ window } />
```

Scope the listener to a container element via a React ref (only fires when focus is inside the container)
``` JSX
const containerRef = useRef(null);

<div ref={containerRef}>
  <KeyBinding onKey={ (e) => { console.log(e.keyCode) } } target={ containerRef } />
</div>
```

Have a look at options.

### Options

All properties except `onKey` are optional.

| Property                         | Description                                                                                                          | Default value |
|----------------------------------|----------------------------------------------------------------------------------------------------------------------|---------------|
| `onKey` (required)               | the function executed after a key event. You're responsible for keeping is stable                                    | n/a           |
| `type`                           | keyup or keydown                                                                                                     | `'keydown'`   |
| `target`                         | the element you want to attach the event to: an **existing** DOM element, a CSS selector (you will need to add `tabIndex='0'` to your element, otherwise the event won't be caught), or a **React ref** (`RefObject<HTMLElement>`) — if a ref is passed and `.current` is `null`, the listener is silently skipped | `document`    |
| `preventInputConflict`           | prevent onKey from firing if you have an onChange on an input, a textarea or a select                                | `false`       |
| `preventContentEditableConflict` | prevent onKey from firing if the user is editing the DOM via contenteditable="true", usually used by WYSIWYG editors | `false`       |
| `preventDefault`                 | prevent event default                                                                                                | `false`       |
| `preventPropagation`             | prevent event propagation                                                                                            | `false`       |

---
_Source: https://npm.io/package/react-keybinding-component · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
