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

@hookies/key-bindings

v2.0.4

Published

A React hook library for adding keyboard shortcuts

Readme

🚀 @hookies/key-bindings

npm version TypeScript GitHub issues

A lightweight React Hook for adding keyboard shortcuts to your application. Easily bind keyboard combinations to functions without extra configuration!


📖 Table of Contents


📦 Installation

Install the package using npm:

npm install @hookies/key-bindings

or using yarn:

yarn add @hookies/key-bindings

🎮 Playground - Test Key Bindings Online

Want to try out keyboard shortcuts before using them in your project? Use our interactive playground to generate the function dynamically!

🛠 🔗 Open Playground

  • Press any key combination (e.g., Meta + X, Ctrl + Shift + A).
  • The playground shows the generated function with the exact keys pressed.
  • Copy the generated function and paste it into your project.

📌 Example Output in Playground
If you press Meta + X, the playground will generate:

useShortcut(["meta", "x"], func);

Just copy and use in your code! 🚀


🔥 Usage

Import the useShortcut or useShortcutExtended hook and bind a keyboard shortcut to an action.

Basic Example: Modifier-Based Shortcuts (useShortcut)

import React, { useState } from "react";
import { useShortcut } from "@hookies/key-bindings";

const App = () => {
  const [message, setMessage] = useState("Press Ctrl+8");

  useShortcut(["Ctrl", "8"], () => setMessage("Shortcut Triggered!"));

  return <h1>{message}</h1>;
};

export default App;

Example: Any Key Combination (useShortcutExtended)

import { useState } from "react";
import { useShortcutExtended } from "@hookies/key-bindings";

const App = () => {
  const [message, setMessage] = useState("Press A + S");

  useShortcutExtended(["a", "s"], () => setMessage("Shortcut Triggered!"));

  return <h1>{message}</h1>;
};

export default App;

🎯 OS-Specific Shortcuts

This library does not auto-adjust shortcuts for Mac vs Windows. If you need platform-specific bindings, use getOS().

import { useShortcut, getOS } from "@hookies/key-bindings";

const os = getOS();
const shortcutKeys = os === "MacOS" ? ["Meta", "8"] : ["Ctrl", "8"];

useShortcut(shortcutKeys, () => console.log("Shortcut Triggered!"));

API Reference

useShortcut(keys: string[], callback: () => void, options?: UseShortcutOptions)

  • keys (string[]) – Array of keys that should trigger the shortcut (e.g., ["Ctrl", "8"]).
  • callback (function) – Function to execute when the shortcut is triggered.
  • options (optional)
    • preventDefault (boolean) – Prevents default browser behavior (default: false).

Example: Prevent Default Behavior

useShortcut(["Ctrl", "S"], () => console.log("Save triggered"), { preventDefault: true });

useShortcutExtended(keys: string[], callback: () => void, options?: UseShortcutOptions)

This hook detects any key combination, including non-modifier keys.

  • keys (string[]) – Array of keys to trigger the shortcut (e.g., ["a", "s"]).
  • callback (function) – Function to execute when the shortcut is triggered.
  • options (optional)
    • preventDefault (boolean) – Prevents default browser behavior (default: false).

Example: Detecting Non-Modifier Key Combinations

useShortcutExtended(["a", "s"], () => console.log("Pressed A + S!"));
useShortcutExtended(["x", "z", "q"], () => console.log("Pressed X + Z + Q!"));

getOS()

Returns the user's operating system as a string: "Windows", "MacOS", "Linux", "Android", "iOS", or "Unknown".

Example

console.log(getOS()); // Outputs: "MacOS"

🛠 Advanced Features (Coming Soon)

  • ✅ Support for multiple shortcut registrations in a single call.
  • ✅ Global shortcut manager.
  • ✅ Dynamic shortcut customization via props.

🚀 Contributing to Hookies Key Bindings

🎉 Thank you for considering contributing to Hookies Key Bindings!
We welcome all contributions, whether it's bug fixes, feature additions, documentation updates, or tests.

1️⃣ Fork the Repository

  • Click on the "Fork" button in the top-right corner of the repository.

  • Clone your forked repository:

    git clone https://github.com/YOUR-USERNAME/-hookies-key-bindings.git
    cd -hookies-key-bindings

2️⃣ Set Up the Project

  • Install dependencies:
    npm install
  • Build the project:
    npm run build

3️⃣ Create a New Branch

Before making any changes, create a new branch:

git checkout -b feature/your-feature-name

or for bug fixes:

git checkout -b fix/your-fix-name

4️⃣ Make Changes and Test

  • Implement your feature or fix.
  • Ensure the build succeeds:
    npm run build
  • If you modified TypeScript files, check types:
    tsc --noEmit

5️⃣ Commit and Push

  • Commit your changes:

    git commit -m "✨ Add new feature: Keyboard shortcuts for Mac"
  • Push your changes:

    git push origin feature/your-feature-name
  • Open a Pull Request (PR) and submit your changes! 🚀


📜 License

This project is licensed under the ISC License.


Support & Feedback

If you like this project, give it a ⭐ on GitHub!
For issues or feature requests, open an issue.