npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

use-keys-bindings

v1.1.2

Published

A custom hook for handling keyboard shortcuts in React apps.

Readme

use-keys-bindings

A lightweight and flexible React hook for handling keyboard shortcuts. This library makes it easy to bind keys or combinations of keys to specific callbacks, with support for modifiers like Ctrl, Shift, Alt, and Meta.

Features

  • Bind single keys or combinations of keys.
  • Support for modifier keys (Ctrl, Shift, Alt, Meta).
  • triggerOnAnyKey option for triggering callbacks on any matching key.
  • Prevent default browser behavior for specific keys.
  • Optional key repeat functionality for continuous actions.
  • Flexible and easy-to-use API.

Installation

npm install use-keys-bindings

or

yarn add use-keys-bindings

Usage

Basic Example

import React from "react";
import { useKeys } from "use-keys-bindings";

const App = () => {
  useKeys({
    keys: ["ArrowRight", "ArrowLeft"],
    triggerOnAnyKey: true,
    callback: (e) => {
      if (e.key === "ArrowRight") console.log("Move Right");
      if (e.key === "ArrowLeft") console.log("Move Left");
    },
  });

  return (
    <div>
      Press Arrow Keys!, triggerOnAnyKey is making it possible to do conditional
      checks in the callback
    </div>
  );
};

export default App;

Continuous Movement Example

import React from "react";
import { useKeys } from "use-keys-bindings";

const App = () => {
  useKeys({
    keys: ["ArrowRight"],
    enableKeyRepeatOnHold: true, // Enable continuous movement
    callback: () => {
      movePlayerRight(); // This will be called continuously while holding the key
    },
  });

  return <div>Hold the right arrow key to move continuously</div>;
};

export default App;

Modifier Keys Example

import React from "react";
import { useKeys } from "use-keys-bindings";

const App = () => {
  useKeys({
    keys: ["s"],
    modifiers: {
      Control: true,
    },
    preventDefault: true,
    callback: () => {
      saveDocument();
    },
  });

  return (
    <div>
      Press Ctrl + S to save, Browser default save option isnt triggered
    </div>
  );
};

export default App;

API

useKeys(...commands)

useKeys accepts one or more command objects. Each command object defines a specific keyboard shortcut.

Command Object Properties

| Property | Type | Required | Default | Description | | ----------------------- | --------------------------------------- | -------- | ------- | ------------------------------------------------------------------------------------- | | keys | Key[] | Yes | — | An array of keys to listen for (e.g., ["ArrowRight", "s"]). | | callback | (e: KeyboardEvent) => void | Yes | — | The function to call when the keys are pressed. | | triggerOnAnyKey | boolean | No | false | If true, the callback will be triggered if any one of the keys is pressed. | | modifiers | Partial<Record<KeyModifier, boolean>> | No | — | An object specifying required modifier keys (e.g., { Control: true, Shift: true }). | | preventDefault | boolean | No | false | Prevents the browser's default behavior for the specified keys. | | enableKeyRepeatOnHold | boolean | No | false | Enables continuous callback triggering while holding the key down. |

Key Modifiers

The modifiers object can include:

  • Control
  • Shift
  • Alt
  • Meta

Best Practices

  • Avoid empty keys arrays: This will throw an error, as all commands must specify at least one key.
  • Use unique combinations: Avoid overlapping key combinations to prevent unexpected behavior.
  • Use enableKeyRepeatOnHold with caution: This feature should primarily be used for:
    • Game movement controls where continuous motion is needed
    • Scrolling or panning interfaces
    • Increment/decrement controls that benefit from continuous input Be aware that enabling this feature will trigger your callback rapidly while the key is held down, so ensure your callback function is optimized for frequent execution.

Links