useKey

React hook for handling keyboard events.

Installation

npm install @mvdlei/hooks

Example

Press the `h` key

Usage

import React from "react";
import { useKey } from "@mvdlei/hooks";
 
const App = () => {
  const [count, setCount] = React.useState(0);
 
  useKey("ArrowUp", () => setCount(count + 1));
  useKey("ArrowDown", () => setCount(count - 1));
 
  return (
    <div>
      <h1>Count: {count}</h1>
    </div>
  );
};

Reference

useKey(key: string, callback: () => void): void;

use-key.ts