Skip to content
프로젝트

소개

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

웹 및 앱 개발

이 수치를 측정하는 방법

측정된 스타 증가

in 24h
0
in 7 days
+1
in 15 days
+1
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original
Using my app is also a way to support me:
Zipora: Zip/RAR/7Z Unarchiver Scap: Screenshot & Markup Edit Screen Test Deskmark Keyzer Vidwall Hub VidCrop Vidwall Mousio Hint Mousio Musicer Audioer FileSentinel FocusCursor Videoer KeyClicker DayBar Iconed Menuist Quick RSS Quick RSS Web Serve Copybook Generator DevTutor for SwiftUI RegexMate Time Passage Iconize Folder Textsound Saver Create Custom Symbols DevHub Resume Revise Palette Genius Symbol Scribe

react-hotkeys

Buy me a coffee Follow On X NPM Downloads CI 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

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>
    )
  }
}

Functional Component Example

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

export default function HotkeysDemo() {
  const [output, setOutput] = useState('Hello, I am a functional component that listens to keydown and keyup');

  const onKeyUp = (keyName, e, handle) => {
    console.log("test:onKeyUp", e, handle);
    setOutput(`onKeyUp ${keyName}`);
  };

  const onKeyDown = (keyName, e, handle) => {
    console.log("test:onKeyDown", keyName, e, handle);
    setOutput(`onKeyDown ${keyName}`);
  };

  return (
    <Hotkeys 
      keyName="shift+a,alt+s" 
      onKeyDown={onKeyDown}
      onKeyUp={onKeyUp}
    >
      <div style={{ padding: "50px" }}>
        {output}
      </div>
    </Hotkeys>
  );
}

With useRef Example

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

export default function HotkeysWithRef() {
  const [output, setOutput] = useState('Press shift+a or alt+s to test');
  const hotkeysRef = useRef(null);

  const onKeyDown = (keyName, e, handle) => {
    console.log("onKeyDown", keyName, e, handle);
    console.log("Current isKeyDown state:", hotkeysRef.current?.isKeyDown);
    setOutput(`Key pressed: ${keyName}`);
  };

  const onKeyUp = (keyName, e, handle) => {
    console.log("onKeyUp", keyName, e, handle);
    console.log("Current isKeyDown state:", hotkeysRef.current?.isKeyDown);
    setOutput(`Key released: ${keyName}`);
  };

  return (
    <Hotkeys
      ref={hotkeysRef}
      keyName="shift+a,alt+s"
      onKeyDown={onKeyDown}
      onKeyUp={onKeyUp}
      allowRepeat={true}
    >
      <div style={{ padding: "50px", border: "1px solid #ccc" }}>
        <p>{output}</p>
        <p>Try pressing and holding shift+a or alt+s</p>
      </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)}
 />

Contributors

As always, thanks to our amazing contributors!

Made with action-contributors.

License

Licensed under the MIT License.

관련 프로젝트 살펴보기

불러오는 중…