2 Star 4 Fork 0

小弟调调 / react-hotkeys

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
contribute
Sync branch
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README
MIT

react-hotkeys

Travis npm package Coveralls

React component to listen to keydown and keyup keyboard events, defining and dispatching keyboard shortcuts. Uses a fork of hotkeys.js for keydown detection of special characters. You give it a keymap of shortcuts & it bind it to the mousetrap singleton. The, it'll unbind it when the component unmounts.

Example

Install

sudo npm i -S react-hot-keys

Demo

Preview demo.

import React, { Component } from 'react';
import Hotkeys from 'react-hot-keys';

export default class HotkeysDemo extends Component {
  constructor(props) {
    super(props);
    this.state = {
      output: 'Hello, I am a component that listens to keydown and keyup of a',
    }
  }
  onKeyUp(keyName, e, handle) {
    console.log("test:onKeyUp", e, handle)
    this.setState({
      output: `onKeyUp ${keyName}`,
    });
  }
  onKeyDown(keyName, e, handle) {
    console.log("test:onKeyDown", keyName, e, handle)
    this.setState({
      output: `onKeyDown ${keyName}`,
    });
  }
  render() {
    return (
      <Hotkeys 
        keyName="shift+a,alt+s" 
        onKeyDown={this.onKeyDown.bind(this)}
        onKeyUp={this.onKeyUp.bind(this)}
      >
        <div style={{ padding: "50px" }}>
          {this.state.output}
        </div>
      </Hotkeys>
    )
  }
}

API

keyName

Supported keys , shift, option, , alt, ctrl, control, command, .

Command()
Control
Option(alt)
Shift
Caps Lock
fn Function key is fn (not supported)
↩︎ return/enter space space keys

onKeyDown

Callback function to be called when user pressed the target buttons space space keys

onKeyUp

Callback function to be called when user key uped the target buttons

License

MIT

react-hotkeys --- [![Travis][build-badge]][build] [![npm package][npm-badge]][npm] [![Coveralls][coveralls-badge]][coveralls] [build-badge]: https://travis-ci.org/jaywcjlove/react-hotkeys.svg?branch=master "Build Badge" [build]: https://travis-ci.org/jaywcjlove/react-hotkeys "Build" [npm-badge]: https://img.shields.io/npm/v/react-hot-keys.svg "Npm Badge" [npm]: https://www.npmjs.org/package/react-hot-keys "npm" [coveralls-badge]: https://img.shields.io/coveralls/jaywcjlove/react-hotkeys/master.svg "Coveralls Badge" [coveralls]: https://coveralls.io/github/jaywcjlove/react-hotkeys "Coveralls" React component to listen to keydown and keyup keyboard events, defining and dispatching keyboard shortcuts. Uses a fork of [hotkeys.js](https://github.com/jaywcjlove/hotkeys) for keydown detection of special characters. You give it a keymap of shortcuts & it bind it to the mousetrap singleton. The, it'll unbind it when the component unmounts. ## Example ### Install ```sh sudo npm i -S react-hot-keys ``` ### Demo Preview [demo](https://jaywcjlove.github.io/react-hotkeys/). ```js import React, { Component } from 'react'; import Hotkeys from 'react-hot-keys'; export default class HotkeysDemo extends Component { constructor(props) { super(props); this.state = { output: 'Hello, I am a component that listens to keydown and keyup of a', } } onKeyUp(keyName, e, handle) { console.log("test:onKeyUp", e, handle) this.setState({ output: `onKeyUp ${keyName}`, }); } onKeyDown(keyName, e, handle) { console.log("test:onKeyDown", keyName, e, handle) this.setState({ output: `onKeyDown ${keyName}`, }); } render() { return ( <Hotkeys keyName="shift+a,alt+s" onKeyDown={this.onKeyDown.bind(this)} onKeyUp={this.onKeyUp.bind(this)} > <div style={{ padding: "50px" }}> {this.state.output} </div> </Hotkeys> ) } } ``` ## API ### keyName Supported keys `⇧`, `shift`, `option`, `⌥`, `alt`, `ctrl`, `control`, `command`, `⌘` . `⌘` Command() `⌃` Control `⌥` Option(alt) `⇧` Shift `⇪` Caps Lock ~~`fn` Function key is `fn` (not supported)~~ `↩︎` return/enter `space` space keys ### onKeyDown Callback function to be called when user pressed the target buttons `space` space keys ### onKeyUp Callback function to be called when user key uped the target buttons ## License MIT

About

React component to listen to keydown and keyup keyboard events, defining and dispatching keyboard shortcuts. expand collapse
JavaScript
MIT
Cancel

Releases

No release

Contributors

All

Activities

Load More
can not load any more
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/jaywcjlove/react-hotkeys.git
git@gitee.com:jaywcjlove/react-hotkeys.git
jaywcjlove
react-hotkeys
react-hotkeys
master

Search

344bd9b3 5694891 D2dac590 5694891