hotkeys-react
TypeScript icon, indicating that this package has built-in type declarations

0.0.4 • Public • Published

hotkeys-react

npm package

说明

本仓库由https://github.com/jaywcjlove/react-hotkeys改进而来,因为原来仓库在react19.0会存在类型问题。由此我想重新改写一遍。

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 hotkey-react

Demo

Preview demo.

import React from 'react';
import { Hotkeys } from 'hotkeys-react';

export function HotkeysDemo() {
  const [output, setOutput] = useState("Hello, I am a component that listens keydown and keyup of a")
  const handleKeyUp = (keyName, e, handle) {
    console.log("test:onKeyUp", e, handle)
    setOutput(`onKeyUp ${keyName}`)
  }
  const handleKeyDown(keyName, e, handle) {
    console.log("test:onKeyDown", keyName, e, handle)
    setOutput(`onKeyDown ${keyName}`)
  }

  return (
      <Hotkeys 
        keyName="shift+a,alt+s" 
        onKeyDown={handleKeyDown}
        onKeyUp={handleKeyUp}
      >
        <div style={{ padding: "50px" }}>
          {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={handleKeyDown}
   onKeyUp={handleKeyUp}
 />

License

Licensed under the MIT License.

Readme

Keywords

none

Package Sidebar

Install

npm i hotkeys-react

Weekly Downloads

1

Version

0.0.4

License

none

Unpacked Size

16.1 kB

Total Files

6

Last publish

Collaborators

  • darwish-yu