@ekwoka/hotkeys
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Hotkeys

This is a very simple package that allows you to register hotkeys to run functions easily!

Installation

pnpm add @ekwoka/hotkeys

Usage

hotkeys({
  'ctrl+c': () => console.log('copying!!!'),
  'ctrl+k': openSearchBar,
  'cmd+p': openCommandPalette,
  'cmd+ctrl+alt+shift+u': activateSuperUser
});

The string accepts ctrl alt cmd and shift as modifiers, or any combination thereof.

The function also returns a function to unregister the hotkeys.

const unregister = hotkeys({
  'ctrl+c': () => console.log('copying!!!'),
  'ctrl+x': () => unregister()
});

This allows it to be easily used inside useEffect for Preact and React to allow easy reactive hotkeys, or cleaning up components registered hotkeys when they are unloaded.

const [counter, setCounter] = useState(0);
useEffect(
  () =>
    hotkeys({
      'ctrl+y': () => setCounter(counter + 1)
    }),
  [counter]
);

Types

Provides Necessary Type declarations for the package, although they are rather simple.

Unsupported Behaviors

  • Registering the '+' key as a hotkey will not work.
  • Not tested with any non-alphanumeric keys.
  • Registering combinations of multiple keys will not work without you customizing your own actions to support them (no cmd+m cmd+w for example).

Package Sidebar

Install

npm i @ekwoka/hotkeys

Weekly Downloads

1

Version

1.0.1

License

ISC

Unpacked Size

5.4 kB

Total Files

11

Last publish

Collaborators

  • ekwoka