- Notifications
You must be signed in to change notification settings - Fork29
React component to handle keyboard events 🔑
ayrton/react-key-handler
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
React component to handle keyboard events (such askeyup,keydown &keypress).
“Happy to see that react-key-handler is SSR safe 👍”[Veselin Todorov](https://github.com/vesln), Chai.js core
$ npm install react-key-handler --save
You can usereact-key-handler library in two flavours:
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 akeyValue,codeandkeyCode property.
Internally theKeyHandler component is used, for a full understanding be sure tocheck outthe implementation.
importReactfrom'react';import{keyHandler,KEYPRESS}from'react-key-handler';functionDemo({ keyValue}){return(<div>{keyValue==='s'&&(<ol><li>hello</li><li>world</li></ol>)}</div>);}exportdefaultkeyHandler({keyEventName:KEYPRESS,keyValue:'s'})(Demo);
The prop types of theKeyHandler component are:
| Name | Type | Required | Default | |
|---|---|---|---|---|
| keyEventName | string | no | 'keyup' | 'keydown','keypress' or'keyup' |
| keyValue | string | yes* | Any givenKeyboardEvent.key | |
| code | string | yes* | Any givenKeyboardEvent.code | |
| keyCode† | number | yes* | Any givenKeyboardEvent.keyCode |
* You should pass at least one of these props.
†Note that the keyCode is frequently browser specific and has therefore be set asdeprecated, seeMDN for details.
importReactfrom'react';importKeyHandler,{KEYPRESS}from'react-key-handler';exportdefaultclassDemoextendsReact.Component{state={showMenu:false};render(){const{ showMenu}=this.state;return(<React.Fragment><KeyHandlerkeyEventName={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 theKeyHandler component are:
| Name | Type | Required | Default | |
|---|---|---|---|---|
| keyEventName | string | no | 'keyup' | 'keydown','keypress' or'keyup' |
| keyValue | string | yes* | Any givenKeyboardEvent.key | |
| code | string | yes* | Any givenKeyboardEvent.code | |
| keyCode† | number | yes* | Any givenKeyboardEvent.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 asdeprecated, seeMDN for details.
This library does not handle key events for form elements such as<input /> and<textarea />.
React does a fine job supporting these already viakeyboard events.
TODO: explain the differences between the different key events.
The three available key events are
keyValueThis corresponds to the true value. This is the value of the key pressedby the user while taking into considerations the state of modifier keyssuch as theshiftKeyas well as the keyboard locale/layoutcodeThis corresponds to the physical key on the keyboard (as opposed to thecharacter generated by pressing the key). In other words, this propertyreturns a value which isn't altered by keyboard layout or the state ofthe modifier keys. The value is a string specific to the key, e.g. 'Digit0'keyCodeThis is similar to code but numeric and alsodeprecated.
We recommend you to use the new Web standardKeyboardEvent.key or theKeyboardEvent.codeover the deprecatedKeyboardEvent.keyCode.
Note that inReactkey is a reserved property, and thus we usekeyValue when referringto thekey property.
Browser support:
There's no need to worry about browser support because internally we normalizedeprecated HTML5keyValue values and translate from legacykeyCode values,similar to how React does this for theirSyntheticKeyboardEvent.
More information:
$ git clone<this repo>$cd react-key-handler$ npm install
To start the server:
$ npm demo
This starts a development server, which will automatically rebuild the demo app as you change files and supports hot module replacement for fast development:
$ open http://localhost:1234
To run all tests:
$ npmtestOr you can run the linters, unit tests and check for type errors individually:
$ 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 asafe, welcoming space for collaboration, and contributors are expected to adhereto theContributor Covenant code of conduct.
_________________< The MIT License > ----------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||About
React component to handle keyboard events 🔑
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors9
Uh oh!
There was an error while loading.Please reload this page.