@devcrush/hotkey

A small library to help creating hotkeys by listening to key-combos.

Usage no npm install needed!

<script type="module">
  import devcrushHotkey from 'https://cdn.skypack.dev/@devcrush/hotkey';
</script>

README

Hotkey

A small library to help creating hotkeys by listening to key-combos.

Install

$ npm install @devcrush/hotkey

Usage

Example hotkeys:

  • "Ctrl+p" (same as "Control+p")
  • "Ctrl+Click"
  • "Ctrl+Up"
  • "Cmd+Click" (same as "Meta+Click")
  • "Left+Down" (same as "ArrowLeft+ArrowDown")
import { hotkey } from "lib/hotkey";

// Subscribing
// ----------------------------------------
// Important!
// Always make sure you pass in function instances instead of anonymous functions,
// as like that it can create multiple subscriptions.

// Good
const callback = () => console.log("Hiya!");
hotkey.on("Cmd+p", callback);

// Bad
hotkey.on("Cmd+p", () => console.log("Hiya!"));

// Unsubscribing
// ----------------------------------------

// Good
const callback = () => console.log("Hiya!");
hotkey.off("Cmd+p", callback);

// Bad
// (this won't do anything)
hotkey.off("Cmd+p", () => console.log("Hiya!"));