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

@tarwich/react-keyboard-shortcuts

v2.0.0

Published

## Why did you do it?

Readme

React Keyboard Shortcuts

Why did you do it?

I wanted a way to easily manage adding keyboard shortcuts to the DOM, while managing them. I've seen things that allow defining keyboard shortcuts for the entire life of the application, but most of the time the keyboard shortcuts that I want to create are very context-sensitive. They only need to occur for one modal or one screen of the application. So this extension brings you the following:

  • Component to register keyboard shortcuts easily
  • As a component, it can be mounted inside any React component
  • If another component is mounted afterwords, it can override shortcuts
  • Since it has a React lifecycle, it will register and unregister shortcuts automatically
  • The React lifecycle causes shortcuts to update automatically in case something in your application changes them after they're registered

How to use it

npm install @tarwich/react-keyboard-shortcuts
import * as React from 'react';
import { render } from 'react-dom';
import { KeyboardShortcuts } from 'react-keyboard-shortcuts';

const root =
  document.querySelector('#root') ||
  document.body.appendChild(document.createElement('div'));
render(
  <KeyboardShortcuts
    shortcuts={{
      '^x': () => {
        console.log('You pressed control X!');
      },
    }}
  />,
  root
);

Simple way to define shortcuts

You can define shortcuts with a simple object, where they object keys are the keyboard keys, and the action is the value. This method is less flexible, but a lot easier to get started with.

<KeyboardShortcuts
  shortcuts={{
    a: () => console.log('You pressed "a"'),
    b: () => console.log('You pressed "b"'),
  }}
/>

Modifier keys

The modifier keys for the keyboard shortcuts are currently the following. If more are needed, then either we'll add definitions for them, or change the approach.

| Code | Actual Key | | :--: | :--------: | | ^ | Control | | ! | Alt | | + | Shift | | # | Meta |

Chords

This shortcut system has heavy emphasis on chords. In fact, I recommend that you do NOT rely heavily on modifiers, but instead chords.

In Gmail, for example, the chord G I means "Go to inbox". You can easily press that combination without needing fancy meta keys, and now that I've gotten used to it, it's much easier to think G <something> will take me anywhere than remembering ^I is inbox, and ^D is drafts (or was it duplicate or delete`?)

In order to make chords, use a space to separate keys. Here are some examples:

| Chord | Description | | ------- | ----------------------------------- | | X | Not a chord. Just press X | | X A | Press X, then press A | | ^X !A | Press Control-X, then press Alt-A | | XA | Error. This is impossible to invoke |

With chords, you can even do "cheat codes"

The Doom cheat code

I D D Q D

Konami cheat code

ArrowUp ArrowUp ArrowDown ArrowDown ArrowLeft ArrowRight ArrowLeft ArrowRight B A Konami!

Debug mode

If you enable debug mode, then you'll get a window on the screen that will help you identify what shortcuts are registered, which ones are overridden, and what keys are being pressed.

Being able to get information on what keys is being pressed is super useful as you can find the codes for non-obvious things like MediaVolumeUp.

<KeyboardShortcuts
  debug={true}
  shortcuts={/* Something goes here */}
/>

Complex configuration

<KeyboardShortcuts
  shortcuts={[
    {chord: 'G I', caption: 'Go to inbox', () => this.gotoInbox() },
    {chord: 'G D', caption: 'Go to drafts', () => this.gotoDrafts() },
  ]}
/>