@seesaw/react-hot-keys

React component to listen to keydown and keyup keyboard events, defining and dispatching keyboard shortcuts.

Usage no npm install needed!

<script type="module">
  import seesawReactHotKeys from 'https://cdn.skypack.dev/@seesaw/react-hot-keys';
</script>

README

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 from 'react';
import Hotkeys from 'react-hot-keys';

export default class HotkeysDemo extends React.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

allowRepeat

allowRepeat?: boolean;

allowRepeat to allow auto repeating key down

disabled

disabled?: boolean;

Disable onKeyDown and onKeyUp events. Default: undefined

filter

INPUT SELECT TEXTAREA default does not handle. filter to return to the true shortcut keys set to play a role, flase shortcut keys set up failure.

<Hotkeys 
  keyName="shift+a,alt+s" 
+  filter={(event) => {
+    return true;
+  }}
  onKeyDown={this.onKeyDown.bind(this)}
  onKeyUp={this.onKeyUp.bind(this)}
/>

License

MIT