百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

react-key-handler

> 前端框架
开源

用于处理键盘事件的 React 组件:key:

389 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

用于处理键盘事件的 React 组件:key:

react-key-handler

React component to handle keyboard events (such as keyup, keydown & keypress).

Testimonials

Veselin Todorov, Chai.js core

Table of Contents

  1. Installation
  2. Usage
    1. Higher-order Components
    2. Component
    3. Form key handling
  3. Key event names
  4. keyValue, code and keyCode
  5. Development
    1. Setup
    2. Getting started
    3. Tests
  6. Contributing
  7. License

Installation

bash
$ npm install react-key-handler --save

Usage

You can use react-key-handler library in two flavours:

  • higher-order components
  • 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.

javascript
import React from 'react';
import { keyHandler, KEYPRESS } from 'react-key-handler';

function Demo({ keyValue }) {
  return (
    

  );
}

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.

Examples

Component

javascript
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 && (
          

            
hello

            
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.

Example

Form key handling

This library does not handle key events for form elements such as and.

React does a fine job supporting these already via keyboard events.

Examples

Key event names

TODO: 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/layout
  • code 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:

W3C Working Draft.

Development

Setup

bash
$ git clone 
$ cd react-key-handler
$ npm install

Getting started

To start the server:

bash
$ 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:

bash
$ open http://localhost:1234

Tests

To run all tests:

bash
$ npm test

Or you can run the linters, unit tests and check for type errors individually:

bash
$ npm run test:lint
$ npm run test:unit
$ npm run test:flow

Contributing

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

bash
 _________________

 -----------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScriptkey-eventkeyboardreactreact-component

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架