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-simple-keyboard

v0.0.1

Published

React hooks for handling keyboard inputs and hotkeys across different platforms

Downloads

22

Readme

useSimpleKeyboard

A simple library of React hooks for handling keyboard inputs and hotkeys across different platforms and languages.

npm version License: MIT

Features

  • 🎯 Cross-platform support - Works consistently across Windows, macOS, and Linux
  • 🌍 Multi-language keyboard support - Handles different keyboard layouts and languages
  • Lightweight - Zero dependencies (except React peer dependency)
  • 🔧 TypeScript support - Full TypeScript definitions included
  • 🎨 Flexible API - Support for single hotkeys and multiple hotkey combinations
  • 🛡️ Event handling - Built-in preventDefault and stopPropagation options

Installation

npm install use-simple-keyboard

Quick Start

import React from 'react';
import { useHotkey } from 'use-simple-keyboard';

function App() {
  // Simple hotkey
  useHotkey(['ctrl', 'c'], () => {
    console.log('Copy triggered!');
  });

  // Platform-specific hotkey (Cmd on Mac, Ctrl on others)
  useHotkey(['meta', 's'], () => {
    console.log('Save triggered!');
  });

  return <div>Press Ctrl+C or Cmd+S (Mac)</div>;
}

API Reference

useHotkey(keys, callback, options?)

The main hook for handling single hotkey combinations.

Parameters

  • keys: string | string[] - The key combination to listen for
  • callback: (event: KeyboardEvent) => void - Function to execute when hotkey is pressed
  • options: UseHotkeyOptions - Optional configuration

Key Formats

You can specify keys in multiple formats:

// Array format
useHotkey(['ctrl', 'c'], callback);
useHotkey(['ctrl', 'shift', 'z'], callback);

// String format with '+' separator
useHotkey('ctrl+c', callback);
useHotkey('ctrl+shift+z', callback);

// Single key
useHotkey('escape', callback);
useHotkey(['f1'], callback);

Supported Keys

Modifier Keys:

  • ctrl / control - Control key
  • meta / cmd / command - Command key (Mac) / Windows key
  • alt - Alt key
  • shift - Shift key

Special Keys:

  • enter / return
  • escape / esc
  • space / spacebar
  • tab
  • backspace
  • delete / del
  • insert
  • home, end
  • pageup, pagedown
  • arrowup, arrowdown, arrowleft, arrowright (or up, down, left, right)

Function Keys:

  • f1 through f12

Regular Keys:

  • Any letter: a, b, c, etc.
  • Any number: 1, 2, 3, etc.
  • Any symbol: +, -, =, etc.

Options

interface UseHotkeyOptions {
  enabled?: boolean; // Default: true
  preventDefault?: boolean; // Default: true
  stopPropagation?: boolean; // Default: false
  target?: HTMLElement | Document | Window | null; // Default: document
  eventType?: 'keydown' | 'keyup' | 'keypress'; // Default: 'keydown'
}

useHotkeys(hotkeys, options?)

Hook for handling multiple hotkeys at once.

import { useHotkeys } from 'use-simple-keyboard';

function Editor() {
  useHotkeys({
    'ctrl+c': () => copy(),
    'ctrl+v': () => paste(),
    'ctrl+z': () => undo(),
    'ctrl+shift+z': () => redo(),
    'ctrl+s': () => save(),
    escape: () => closeModal(),
  });

  return <div>Text Editor</div>;
}

Usage Examples

Basic Hotkeys

import { useHotkey } from 'use-simple-keyboard';

function BasicExample() {
  useHotkey(['ctrl', 'c'], () => alert('Copy!'));
  useHotkey(['ctrl', 'v'], () => alert('Paste!'));
  useHotkey('escape', () => alert('Escape pressed!'));

  return <div>Try Ctrl+C, Ctrl+V, or Escape</div>;
}

Platform-Specific Hotkeys

function PlatformExample() {
  // Use 'meta' for Cmd on Mac, Ctrl on others
  useHotkey(['meta', 's'], () => save());

  // Or handle both explicitly
  useHotkey(['ctrl', 's'], () => save()); // Windows/Linux
  useHotkey(['cmd', 's'], () => save()); // Mac

  return <div>Press Cmd+S (Mac) or Ctrl+S (Windows/Linux)</div>;
}

Conditional Hotkeys

function ConditionalExample() {
  const [isEditing, setIsEditing] = useState(false);

  useHotkey(['ctrl', 'e'], () => setIsEditing(true), {
    enabled: !isEditing,
  });

  useHotkey('escape', () => setIsEditing(false), {
    enabled: isEditing,
  });

  return (
    <div>
      {isEditing ? 'Editing mode - Press Escape' : 'Press Ctrl+E to edit'}
    </div>
  );
}

Scoped Hotkeys

function ScopedExample() {
  const modalRef = useRef<HTMLDivElement>(null);

  // Only listen for hotkeys within the modal
  useHotkey('escape', () => closeModal(), {
    target: modalRef.current,
  });

  return (
    <div ref={modalRef} tabIndex={-1}>
      Modal content - Press Escape to close
    </div>
  );
}

Complex Combinations

function ComplexExample() {
  // Multiple modifier keys
  useHotkey(['ctrl', 'shift', 'alt', 'r'], () => {
    console.log('Super reload!');
  });

  // Function keys
  useHotkey('f12', () => openDevTools());

  // Number keys
  useHotkey(['ctrl', '1'], () => switchToTab(1));
  useHotkey(['ctrl', '2'], () => switchToTab(2));

  return <div>Try various key combinations</div>;
}

Global App Hotkeys

function App() {
  useHotkeys({
    // File operations
    'ctrl+n': () => newFile(),
    'ctrl+o': () => openFile(),
    'ctrl+s': () => saveFile(),

    // Edit operations
    'ctrl+z': () => undo(),
    'ctrl+y': () => redo(),
    'ctrl+shift+z': () => redo(), // Alternative redo

    // Navigation
    'ctrl+f': () => openSearch(),
    'ctrl+g': () => findNext(),
    'ctrl+shift+g': () => findPrevious(),

    // Application
    f11: () => toggleFullscreen(),
    'ctrl+shift+i': () => openDevTools(),
  });

  return <YourAppContent />;
}

Cross-Platform Considerations

The library automatically handles platform differences:

  • macOS: Uses meta key (Cmd) for system shortcuts
  • Windows/Linux: Uses ctrl key for system shortcuts
  • Key normalization: Handles different key representations across browsers
  • Language support: Works with different keyboard layouts and languages

TypeScript Support

The library is written in TypeScript and includes full type definitions:

import { useHotkey, UseHotkeyOptions, KeyEvent } from 'use-simple-keyboard';

const options: UseHotkeyOptions = {
  enabled: true,
  preventDefault: true,
  target: document.body,
};

useHotkey(
  ['ctrl', 'c'],
  (event: KeyboardEvent) => {
    // event is properly typed
    console.log(event.key);
  },
  options
);

Browser Support

  • Chrome/Chromium 60+
  • Firefox 55+
  • Safari 12+
  • Edge 79+

License

MIT