Socket
Book a DemoInstallSign in
Socket

@luxonauta/use-hotkeys

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

@luxonauta/use-hotkeys

🌭 A React hook for registering one or more keyboard shortcuts

latest
Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

useHotkeys

A React hook for handling keyboard shortcuts and hotkey sequences.

npm version License: MIT React >=18

Installation

npm install @luxonauta/use-hotkeys

Basic Usage

import useHotkeys from "@luxonauta/use-hotkeys";

export const Component = () => {
  useHotkeys("Control+k", (event) => {
    event.preventDefault();
    console.log("Control+K was pressed");
  });

  return <p>Press Control+K</p>;
};

Usage with State

import { useHotkeysWithState } from "@luxonauta/use-hotkeys";

export const Component = () => {
  const indicator = useHotkeysWithState(
    "Escape",
    () => {
      console.log("Escape pressed");
    },
    { returnState: true }
  );

  return (
    <div>
      <p>Press Escape</p>
      <pre>{JSON.stringify(indicator, null, 2)}</pre>
    </div>
  );
};

API Reference

useHotkeys

useHotkeys(
  keys: string | string[],
  callback: (event: KeyboardEvent) => void,
  options?: HotkeysOptions
): void

useHotkeysWithState

useHotkeysWithState(
  keys: string | string[],
  callback: (event: KeyboardEvent) => void,
  options?: HotkeysOptions & { returnState: true }
): HotkeyIndicatorState

Options (HotkeysOptions)

OptionTypeDefaultDescription
eventType"keydown" | "keyup" | "keypress""keydown"Which event to listen for
targetEventTarget | nullwindowDOM target for the event listener
eventOptionsAddEventListenerOptions | booleanfalseOptions for addEventListener
enabledbooleantrueEnable or disable the hotkey
requireCtrlbooleanfalseRequire the Control key
requireMetabooleanfalseRequire the Meta key (⌘ on Mac)
requireShiftbooleanfalseRequire the Shift key
requireAltbooleanfalseRequire the Alt key
returnStatebooleanfalseWhether to return state info (isActive, etc)

Indicator State (HotkeyIndicatorState)

PropertyTypeDescription
isActivebooleanWhether the key is currently pressed
lastKeystring?Last key pressed
lastEventTypestring?Last event type (keydown, keyup, keypress)
pressCountnumberTotal number of times triggered
lastTriggeredAtnumber?Timestamp of last trigger
isCombinationActivebooleanWhether required modifiers are pressed

Pattern Utilities

The package also includes helpers for parsing and formatting hotkey patterns:

import {
  matchHotkey,
  matchAnyHotkey,
  createHotkeyHandler,
  formatPattern
} from "@luxonauta/use-hotkeys";
  • matchHotkey(event, pattern, options?): Check if an event matches a pattern like Control + K;
  • matchAnyHotkey(event, [patterns], options?): Check against multiple patterns.
  • createHotkeyHandler(patterns, handler, options?): Create an event handler for simple or sequence-based hotkeys.
  • formatPattern(pattern, options?): Format a pattern for display:
    formatPattern("Control+k"); // => "Ctrl + K"
    formatPattern("g g"); // => "G then G"
    

Advanced Usage

Hotkey Sequences

import { createHotkeyHandler } from "@luxonauta/use-hotkeys";

export const Component = () => {
  React.useEffect(() => {
    const handler = createHotkeyHandler(
      { patterns: "g g", sequenceTimeoutMs: 600 },
      () => {
        console.log("Sequence G then G matched");
      }
    );

    window.addEventListener("keydown", handler);

    return () => window.removeEventListener("keydown", handler);
  }, []);

  return <p>Press "g g"</p>;
};

Formatting for Display

import { formatPattern } from "@luxonauta/use-hotkeys";

console.log(formatPattern("Control+k")); // Ctrl + K
console.log(formatPattern("Meta+Shift+p")); // ⌘⇧P (on macOS)

Best Practices

  • Scope your hotkeys: Pass a target to bind hotkeys only inside a component;
  • Respect accessibility: Do not override default browser shortcuts without a good reason;
  • Keep patterns simple: Complex sequences can confuse users.

License

MIT

Keywords

react

FAQs

Package last updated on 16 Sep 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts