/use-keydown

A custom React hook to fire an event when user press one, or more specific keys.

Primary LanguageJavaScriptMIT LicenseMIT

use-keydown

GitHub license npm version CircleCI Status Coverage

A custom React hook to fire an event when user press one, or more specific keys.

Install

npm i @bscop/use-keydown

Usage

Default behavior:

import useKeydown from "@bscop/use-keydown";

useKeydown(
  (event) => {
    console.log(">", event.code);
  }
);

Custom behavior:

import useKeydown from "@bscop/use-keydown";

useKeydown(
  (event) => {
    console.log(">", event.code);
  },
  {
    active: true,
    keys: [
      "ArrowUp",
      "ArrowDown"
    ]
  }
);
  • active: when is set to false the hook won't register the event listener.

  • keys: defines the key codes for which the handler will be executed. You can omit it, to listen for all the possible keys.

Contribute

Read the guidelines.

Run tests

npm test

Coverage

Coverage reports are hosted on codecov.

npm run badge:coverage -- --token=<guid>

Bruno Scopelliti
www.brunoscopelliti.com