Skip to content

createKeybindRegistry

function createKeybindRegistry<S>(scopes): KeybindRegistry<S>;

Create a static keybind registry from a scope→keybinds mapping.

The returned registry object exposes the raw data together with helper methods for command lookup, help generation, and scope queries.

Type Parameter
S extends string
ParameterTypeDescription
scopesRecord<S, KeybindDef[]>A record mapping scope names to arrays of keybind definitions.

KeybindRegistry<S>

A frozen KeybindRegistry instance.

import { createKeybindRegistry } from "@semos-labs/glyph";
const registry = createKeybindRegistry({
global: [
{ key: "?", display: "?", description: "Show help", action: "openHelp", command: "help" },
{ key: ":", display: ":", description: "Open command bar", action: "openCommand" },
{ key: "q", display: "q", description: "Quit", action: "quit", command: "quit" },
],
list: [
{ key: "j", display: "j / ↓", description: "Next item", action: "next" },
{ key: "down", display: "j / ↓", description: "Next item", action: "next" },
{ key: "k", display: "k / ↑", description: "Previous item", action: "prev" },
{ key: "up", display: "k / ↑", description: "Previous item", action: "prev" },
{ key: "return", display: "Enter", description: "Open item", action: "open" },
],
});

A single keybind definition within a scope.

const def: KeybindDef = {
key: "shift+d",
display: "D",
description: "Delete item",
action: "delete",
command: "delete",
};
PropertyTypeDescription
actionstringAction identifier used to look up the handler in a handlers map.
command?stringOptional command name for the command palette. Omit to exclude from commands.
descriptionstringShort description of what the keybind does.
displaystringHuman-readable display string shown in help dialogs (e.g. "D", "Ctrl+u").
keystringKey combo string used for matching (e.g. "shift+d", "ctrl+u", ":"). Leave empty ("") for command-only entries with no keyboard shortcut.

A command entry extracted from the registry.

PropertyTypeDescription
actionstringAction identifier to dispatch.
descriptionstringDescription of the command.
namestringThe command name (from KeybindDef.command).

A typed keybind registry with helper methods.

Created by createKeybindRegistry. Provides access to the raw scope→keybind mapping and convenience methods for command lookup, help generation, and more.

const commands = registry.getAllCommands();
const match = registry.findCommand("goto tomorrow");
const help = registry.getKeybindsForHelp("timeline");
Type ParameterDefault type
S extends stringstring
findCommand(input):
| {
action: string;
args?: string;
name: string;
}
| null;

Find a command by user input text.

Supports exact matches and parameterised commands (e.g. "goto <date>" matches input "goto tomorrow" with args = "tomorrow").

ParameterTypeDescription
inputstringRaw user input from the command bar.

| { action: string; args?: string; name: string; } | null

Matched command with optional args, or null.


getAllCommands(): CommandDef[];

Collect every keybind that has a command field, sorted by name.

CommandDef[]

Sorted array of command definitions.


getKeybindsForHelp(context, options?): {
keybinds: KeybindDef[];
title: string;
}[];

Build sections for a help dialog: context-specific, related sub-modes, then global keybinds.

ParameterTypeDescription
contextSThe primary scope to display.
options?HelpOptions<S>Optional related scopes and titles.

{ keybinds: KeybindDef[]; title: string; }[]

Ordered sections for rendering.


getKeybindsForScope(scope): KeybindDef[];

Get de-duplicated keybinds for a scope (unique by display).

ParameterTypeDescription
scopeSScope name.

KeybindDef[]

Keybinds with duplicate display values removed.

PropertyModifierTypeDescription
scopesreadonlyReadonly<Record<S, KeybindDef[]>>The raw scope→keybinds mapping.

Options for KeybindRegistry.getKeybindsForHelp.

Type Parameter
S extends string
PropertyTypeDescription
globalScope?SWhich scope is the global scope (always appended last). Default: "global" if it exists in the registry, otherwise omitted.
related?S[]Related sub-mode scopes to include after the primary scope.
scopeTitles?Partial<Record<S, string>>Human-readable titles for scopes. Falls back to the scope key.