Skip to content

API

Create a new Hotkeys instance. Starts listening immediately.

import { createHotkeys } from "@hotter-keys/core";
const hk = createHotkeys();
OptionTypeDefaultDescription
targetEventTargetdocumentElement to listen on
scopestring"*"Initial scope
sequenceTimeoutnumber1000Ms between chords before resetting sequence progress

Register a shortcut. Returns an unsubscribe function.

const unsub = hk.add("mod+k", () => console.log("fired"));
unsub(); // removes the binding

shortcut — a string ("mod+k"), a Shortcut object, or a ShortcutSequence array.

options — see BindingOptions.

Register multiple shortcuts at once. Returns a single unsubscribe function that removes all of them.

const unsub = hk.addMany({
"mod+s": () => save(),
"mod+k mod+c": () => toggleComment(),
});

Remove all bindings matching a shortcut.

hk.remove("mod+k");

Remove all bindings.

OptionTypeDefaultDescription
scopestringOnly fires in the matching scope
layerstring"global"Layer for priority ordering
preventDefaultbooleantrueCall event.preventDefault()
stopPropagationbooleanfalseCall event.stopPropagation()
enableInInputbooleanfalseFire in <input>, <textarea>, contenteditable
requireResetbooleanfalseFire once per press cycle (all keys must release first)
crossPlatformbooleantrueAuto-translate ctrlmeta per platform

Returns the current scope string.

Set the active scope. Only bindings with a matching scope (or no scope) will fire.

Returns a readonly copy of the current layer stack.

hk.getLayers(); // ["global", "editor"]

Push a layer onto the stack. No-op if the layer is already present.

Pop the topmost layer (except "global"). Returns the popped name, or undefined if only "global" remains.

Remove a specific layer by name. Returns true if found, false otherwise. Cannot remove "global".

Subscribe to layer stack changes. Returns an unsubscribe function.

const unsub = hk.onLayerChange((layers) => {
console.log(layers); // ["global", "editor"]
});

Returns a readonly array of currently pressed keys.

Subscribe to held-keys changes. Fires on every keydown/keyup. Returns an unsubscribe function.

Watch if a specific key is held alone. The listener fires with true when the key is the sole key pressed, and false when another key is pressed alongside it or when it’s released.

hk.onKeyHold("shift", (held) => {
// Show shortcut hints while Shift is held alone
overlay.visible = held;
});

Returns a readonly array of all registered Binding objects. Useful for devtools and debugging.

Start listening for keyboard events. Called automatically by the constructor.

Stop listening without destroying state. Call start() to resume.

Full cleanup: stops listening, removes all bindings, clears all state.