react-piano-keys

Handle any hotkey in react

Usage no npm install needed!

<script type="module">
  import reactPianoKeys from 'https://cdn.skypack.dev/react-piano-keys';
</script>

README

react-piano-keys

npm version PRs Welcome

A React hotkeys librairy in less than 4KB.

Installation

npm install --save react-piano-keys

Usage

import useKeys from 'react-piano-keys'

function MyCoolComponent() {
  useKeys(window, 'cmd+b', () => console.log('bold!'))

  return (
    <div>
      Press cmd+b for bold!
    </div>
  )
}
import { useRef } from 'react'
import useKeys from 'react-piano-keys'

function MyOtherCoolComponent() {
  const divRef = useRef()

  useKeys(divRef.current, 'up up down down left right left right b a', () => console.log('konami code!'))

  return (
    <div ref={divRef}>
      What could the code be?
    </div>
  )
}

Available descriptors: ctrl shift alt altgr cmd enter tab space backspace escape capslock up down left right plus contextmenu delete insert pause home end numlock

Usage with vanilla JavaScript

Checkout piano-keys.

Contributing

Yes, thank you.

License

MIT