React component to handle keyboard events (such as keyup
, keydown
& keypress
).
Table of Contents InstallationâHappy to see that react-key-handler is SSR safe :+1:â
[Veselin Todorov](https://github.com/vesln), Chai.js core
Usage$ npm install react-key-handler --save
You can use react-key-handler
library in two flavours:
This library includes two similar higher-order components, but with a different puprose:
Higher-order Component PurposekeyHandler
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.
import React from 'react';
import { keyHandler, KEYPRESS } from 'react-key-handler';
Â
function Demo({ keyValue }) {
  return (
    <div>
      {keyValue === 's' && (
        <ol>
          <li>hello</li>
          <li>world</li>
        </ol>
      )}
    </div>
  );
}
Â
export default keyHandler({ keyEventName: KEYPRESS, keyValue: 's' })(Demo);
The prop types of the KeyHandler
component are:
* 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.
Componentimport 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 (
      <React.Fragment>
        <KeyHandler
          keyEventName={KEYPRESS}
          keyValue="s"
          onKeyHandle={this.toggleMenu}
        />
Â
        {showMenu && (
          <ol>
            <li>hello</li>
            <li>world</li>
          </ol>
        )}
      </React.Fragment>
    );
  },
Â
  toggleMenu = (event) => {
    event.preventDefault();
Â
    this.setState({ showMenu: !this.state.showMenu });
  };
}
The prop types of the KeyHandler
component are:
'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.
Form key handlingThis library does not handle key events for form elements such as <input />
and <textarea />
.
React does a fine job supporting these already via keyboard events.
Key event namesTODO: explain the differences between the different key events.
keyValue
, code
and keyCode
The three available key events are
keyValue
This corresponds to the true value. This is the value of the key pressed by the user while taking into considerations the state of modifier keys such as the shiftKey
as well as the keyboard locale/layoutcode
This corresponds to the physical key on the keyboard (as opposed to the character generated by pressing the key). In other words, this property returns a value which isn't altered by keyboard layout or the state of the modifier keys. The value is a string specific to the key, e.g. 'Digit0'keyCode
This is similar to code but numeric and also deprecated.We recommend you to use the new Web standard KeyboardEvent.key or the KeyboardEvent.code over the deprecated KeyboardEvent.keyCode.
Note that in React key
is a reserved property, and thus we use keyValue
when referring to the key
property.
Browser support:
There's no need to worry about browser support because internally we normalize deprecated HTML5 keyValue
values and translate from legacy keyCode
values, similar to how React does this for their SyntheticKeyboardEvent
.
More information:
Development SetupGetting started$ git clone <this repo>
$ cd react-key-handler
$ npm install
To start the server:
This starts a development server, which will automatically rebuild the demo app as you change files and supports hot module replacement for fast development:
Tests$ open http://localhost:1234
To run all tests:
Or you can run the linters, unit tests and check for type errors individually:
Contributing$ npm run test:lint
$ npm run test:unit
$ npm run test:flow
Bug reports and pull requests are welcome on GitHub. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License _________________
< The MIT License >
-----------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4