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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@rdub/use-hotkeys

v0.2.0

Published

Keyboard shortcuts with runtime-editable keybindings and capture UI

Readme

@rdub/use-hotkeys

npm version

React hooks for keyboard shortcuts with runtime editing and key capture.

Installation

pnpm add @rdub/use-hotkeys

Usage

Basic shortcuts

import { useHotkeys } from '@rdub/use-hotkeys'

const HOTKEYS = {
  't': 'setTemp',
  'c': 'setCO2',
  'ctrl+s': 'save',
  'shift+?': 'showHelp',
}

function App() {
  useHotkeys(HOTKEYS, {
    setTemp: () => setMetric('temp'),
    setCO2: () => setMetric('co2'),
    save: () => handleSave(),
    showHelp: () => setShowHelp(true),
  })
}

Recording key combinations

import { useRecordHotkey } from '@rdub/use-hotkeys'

function KeybindingButton() {
  const { isRecording, startRecording, display, activeKeys } = useRecordHotkey({
    onCapture: (combo, display) => {
      console.log(`Captured: ${display.display}`) // "⌘⇧K" on Mac
      saveBinding(display.id) // "meta+shift+k"
    }
  })

  return (
    <button onClick={() => startRecording()}>
      {isRecording
        ? (activeKeys ? formatCombination(activeKeys).display : 'Press keys...')
        : (display?.display ?? 'Click to set')}
    </button>
  )
}

API

useHotkeys(keymap, handlers, options?)

Register keyboard shortcuts.

  • keymap: Record<string, string | string[]> - maps key combos to action names
  • handlers: Record<string, (e: KeyboardEvent) => void> - maps action names to functions
  • options:
    • enabled?: boolean - enable/disable (default: true)
    • target?: HTMLElement | Window - event target (default: window)
    • preventDefault?: boolean - prevent default on match (default: true)
    • stopPropagation?: boolean - stop propagation on match (default: true)
    • enableOnFormTags?: boolean - fire in inputs/textareas (default: false)

useRecordHotkey(options?)

Capture key combinations from user input.

  • options:
    • onCapture?: (combo, display) => void - called when combination captured
    • onCancel?: () => void - called when recording cancelled
    • preventDefault?: boolean - prevent default during capture (default: true)

Returns:

  • isRecording: boolean
  • startRecording: () => () => void - returns cancel function
  • cancel: () => void
  • combination: KeyCombination | null
  • display: KeyCombinationDisplay | null
  • activeKeys: KeyCombination | null - keys currently held (for live UI feedback)

useEditableHotkeys(defaults, handlers, options?)

Wraps useHotkeys with editable keybindings and localStorage persistence.

import { useEditableHotkeys } from '@rdub/use-hotkeys'

const { keymap, setBinding, reset, overrides } = useEditableHotkeys(
  { 't': 'setTemp', 'c': 'setCO2' },
  { setTemp: () => setMetric('temp'), setCO2: () => setMetric('co2') },
  { storageKey: 'app-hotkeys' }
)
  • defaults: Default keymap
  • handlers: Action handlers (same as useHotkeys)
  • options:
    • storageKey?: string - localStorage key for persistence (omit to disable)
    • disableConflicts?: boolean - disable keys with multiple actions bound (default: true)
    • Plus all useHotkeys options

Returns:

  • keymap: HotkeyMap - current merged keymap
  • setBinding: (action, key) => void - update a single binding
  • setKeymap: (overrides) => void - update multiple bindings
  • reset: () => void - clear all overrides
  • overrides: Partial<HotkeyMap> - user overrides only
  • conflicts: Map<string, string[]> - keys with multiple actions bound
  • hasConflicts: boolean - whether any conflicts exist

<ShortcutsModal>

Display keyboard shortcuts in a modal (opens with ? by default).

import { ShortcutsModal } from '@rdub/use-hotkeys'

<ShortcutsModal
  keymap={HOTKEYS}
  descriptions={{ 'metric:temp': 'Switch to temperature' }}
  groups={{ metric: 'Metrics', time: 'Time Range' }}
/>

Props:

  • keymap: HotkeyMap - shortcuts to display
  • descriptions?: Record<string, string> - action descriptions
  • groups?: Record<string, string> - group prefix → display name
  • isOpen?: boolean - controlled visibility
  • onClose?: () => void - close callback
  • openKey?: string - key to open (default: '?')
  • autoRegisterOpen?: boolean - auto-register open key (default: true)
  • children?: (props) => ReactNode - custom render function

<KeybindingEditor>

UI for viewing and editing keybindings with conflict detection.

import { KeybindingEditor } from '@rdub/use-hotkeys'

<KeybindingEditor
  keymap={keymap}
  defaults={DEFAULT_KEYMAP}
  descriptions={{ save: 'Save document' }}
  onChange={(action, key) => setBinding(action, key)}
  onReset={() => reset()}
/>

Props:

  • keymap: HotkeyMap - current keymap
  • defaults: HotkeyMap - default keymap (for reset)
  • descriptions?: Record<string, string> - action descriptions
  • onChange: (action, key) => void - binding change callback
  • onReset?: () => void - reset callback
  • children?: (props) => ReactNode - custom render function

Utilities

import {
  formatCombination,
  parseCombinationId,
  findConflicts,
  hasConflicts,
} from '@rdub/use-hotkeys'

formatCombination({ key: 'k', modifiers: { meta: true, shift: true, ctrl: false, alt: false }})
// → { display: "⌘⇧K", id: "meta+shift+k" } on Mac
// → { display: "Win+Shift+K", id: "meta+shift+k" } elsewhere

parseCombinationId('ctrl+shift+k')
// → { key: 'k', modifiers: { ctrl: true, shift: true, alt: false, meta: false }}

// Conflict detection
const keymap = { 't': 'setTemp', 't': 'toggleTheme' } // same key!
findConflicts(keymap) // → Map { 't' => ['setTemp', 'toggleTheme'] }
hasConflicts(keymap)  // → true

Key format

Modifier keys: ctrl, alt, shift, meta (or cmd/command on Mac)

Examples:

  • 't' - just T key
  • 'shift+t' - Shift+T
  • 'ctrl+shift+k' - Ctrl+Shift+K
  • 'meta+s' - Cmd+S on Mac, Win+S elsewhere

Examples

Projects using @rdub/use-hotkeys:

  • runsascoded/awair – Air quality dashboard with keyboard shortcuts for metric switching

    Press ? to see the shortcuts modal, or use single keys to switch metrics:

    • t / c / h / p / v – Temperature / CO₂ / Humidity / PM2.5 / VOC
    • 1 / 3 / 7 / m – 1 day / 3 days / 1 week / 1 month time range

See also

ROADMAP.md - feature overview and future ideas.

License

MIT