# react-key-handler 🔑 [![npm version](https://img.shields.io/npm/v/react-key-handler.svg)](https://www.npmjs.com/package/react-key-handler) [![License](https://img.shields.io/npm/l/react-key-handler.svg)](https://www.npmjs.com/package/react-key-handler) [![Build Status](https://travis-ci.org/ayrton/react-key-handler.svg?branch=master)](https://travis-ci.org/ayrton/react-key-handler) React component to handle keyboard events (such as `keyup`, `keydown` & `keypress`). ## Testimonials >
“Happy to see that react-key-handler is SSR safe :+1:”
> [Veselin Todorov](https://github.com/vesln), Chai.js core ## Table of Contents 1. [Installation](#installation) 2. [Usage](#usage) 1. [Higher-order Components](#higher-order-components) 2. [Component](#component) 3. [Form key handling](#form-key-handling) 3. [Key event names](#key-event-names) 4. [`keyValue`, `code` and `keyCode`](#keyvalue-code-and-keycode) 5. [Development](#development) 1. [Setup](#setup) 2. [Getting started](#getting-started) 3. [Tests](#tests) 6. [Contributing](#contributing) 7. [License](#license) ## Installation ```sh $ npm install react-key-handler --save ``` ## Usage You can use `react-key-handler` library in two flavours: - [higher-order components](#higher-order-components) - [component](#component) ### Higher-order Components This library includes two similar higher-order components, but with a different puprose: | Higher-order Component | Purpose | | ---------------------- | ------------------- | | `keyHandler` | Handles key changes | | `keyToggleHandler` | Handles key toggles | Both have the same API and will decorate the given component with a `keyValue`, `code` and `keyCode` property. Internally the `KeyHandler` component is used, for a full understanding be sure to check out [the implementation](lib/key-handler.js). ```jsx import React from 'react'; import { keyHandler, KEYPRESS } from 'react-key-handler'; function Demo({ keyValue }) { return (
{keyValue === 's' && (
  1. hello
  2. world
)}
); } export default keyHandler({ keyEventName: KEYPRESS, keyValue: 's' })(Demo); ``` The prop types of the `KeyHandler` component are: | Name | Type | Required | Default | | | --------------- | ------ | ---------- | --------- | -------------------------------------- | | keyEventName | string | no | `'keyup'` | `'keydown'`, `'keypress'` or `'keyup'` | | keyValue | string | yes **\*** | | Any given [KeyboardEvent.key] | | code | string | yes **\*** | | Any given [KeyboardEvent.code] | | keyCode† | number | yes **\*** | | Any given [KeyboardEvent.keyCode] | **\*** You should pass at least one of these props. † _Note_ that the keyCode is frequently browser specific and has therefore be set as deprecated, see [MDN for details](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode). [Examples](demo/components/examples/decorators/) ### Component ```jsx import React from 'react'; import KeyHandler, { KEYPRESS } from 'react-key-handler'; export default class Demo extends React.Component { state = { showMenu: false }; render() { const { showMenu } = this.state; return ( {showMenu && (
  1. hello
  2. world
)}
); }, toggleMenu = (event) => { event.preventDefault(); this.setState({ showMenu: !this.state.showMenu }); }; } ``` The prop types of the `KeyHandler` component are: | Name | Type | Required | Default | | | --------------- | -------- | ---------- | --------- | ------------------------------------------------ | | keyEventName | string | no | `'keyup'` | `'keydown'`, `'keypress'` or `'keyup'` | | keyValue | string | yes **\*** | | Any given [KeyboardEvent.key] | | code | string | yes **\*** | | Any given [KeyboardEvent.code] | | keyCode† | number | yes **\*** | | Any given [KeyboardEvent.keyCode] | | onKeyHandle | function | yes | | Function that is called when they key is handled | **\*** You should pass at least one of these props. † _Note_ that the keyCode is frequently browser specific and has therefore be set as deprecated, see [MDN for details](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode). [Example](demo/components/examples/component/index.js) ### Form key handling This library does not handle key events for form elements such as `` and `