@androidtui/keymap

中文文档
0.8.0发布于 2026/10/30MITTypeScriptESM + CJS近 30 天下载 1888

Keybindings and command sequences for OpenTUI applications on Android and Termux

原始使用文档
文档目录9 节
  1. Highlights
  2. Addons
  3. Entry Points
  4. Usage
  5. Adapters
  6. Formatting Keys
  7. Re-entry
  8. Installation
  9. Development

@opentui/keymap

A host-agnostic keymap engine for terminal and DOM apps. Same core, multiple adapters.

It models keybindings as priority-ordered, focus-scoped layers attached to targets (terminal renderables or DOM elements). The core is intentionally bare; everything beyond raw key dispatch is opt-in via addons, parsers, expanders, resolvers, and field compilers.

Highlights

  • Host-agnostic core over a small KeymapHost interface with host metadata, focus, parent traversal, target destruction, key press/release, optional raw input, and synthetic command events.
  • Target-aware focus routing for global layers plus local focus / focus-within layers that follow the active target's parent chain.
  • Layered bindings with priority ordering, newest-first ties, fallthrough, and preventDefault control.
  • Branch-aware multi-key sequences over flat compiled bindings, with a public pending-sequence API, synchronous pendingSequence events, active continuation queries, and automatic invalidation on focus changes.
  • Programmable exact-vs-prefix disambiguation (e.g. g vs gg) with runExact, continueSequence, clear, and deferred AbortSignal + sleep decisions. Ships a Neovim-style timeout resolver.
  • Pluggable binding language: stackable binding parsers, key expanders, layer-binding transformers, binding transformers, command resolvers, command transformers, and ordered event-match resolvers.
  • Extensible schema and activation: register custom fields on layers, bindings, and commands. Field compilers can emit attrs; all field kinds can gate activation via require(...) and activeWhen(matcher).
  • Reactive matchers with subscription-driven state notifications, plus React store and Solid signal helpers.
  • Raw and key intercepts before and after normal binding dispatch, including pre-binding consume({ preventDefault, stopPropagation }), post-dispatch handled/no-match outcomes, and raw input stop() handling.
  • Command catalog and dispatch with named commands, inline command handlers, command chains, namespaces, search, visibility tiers (registered / reachable / active), binding queries, runCommand, and focus-aware dispatchCommand.
  • Opt-in graph snapshots and diagnostics for layers, commands, bindings, sequence nodes, pending paths, inactive reasons, shadowing, stable warning/error codes, and lint-style layer analyzers.
  • Broad key coverage in the default parser, including function keys, navigation/editing keys, numpad keys, media keys, left/right modifiers, super, hyper, and literal + bindings.
  • Platform-aware modifier aliases via registerModBindings, resolving mod+... from host metadata while preserving display strings.

Addons

@opentui/keymap/addons ships ready-made building blocks:

  • registerDefaultKeys — ctrl+shift+s style parser and event matching.
  • registerLeader, registerTimedLeader — leader tokens with optional timeout.
  • registerEmacsBindings — ctrl+x ctrl+s chords.
  • registerExCommands — :write-style commands with aliases and nargs.
  • registerCommaBindings, registerModBindings, registerAliasesField, registerBindingOverrides.
  • registerEnabledFields, registerMetadataFields (desc, group, title, category).
  • registerNeovimDisambiguation, registerEscapeClearsPendingSequence, registerBackspacePopsPendingSequence.
  • registerDeadBindingWarnings, registerUnresolvedCommandWarnings.

@opentui/keymap/addons/opentui adds OpenTUI-specific pieces: base-layout matching via event.baseCode for non-Latin keys and the digit row (the same rule as Core), and pre-wired textarea / edit-buffer commands. Direct event matches win before fallback matches, even across layers.

Entry Points

  • @opentui/keymap — core API
  • @opentui/keymap/addons — universal addons
  • @opentui/keymap/addons/opentui — universal + OpenTUI addons
  • @opentui/keymap/extras/graph — graph snapshot helpers for debug and graph UIs
  • @opentui/keymap/testing — host-agnostic fake keymap host and diagnostics for addon tests
  • @opentui/keymap/html — HTML adapter
  • @opentui/keymap/opentui — OpenTUI adapter
  • @opentui/keymap/react — KeymapProvider, useKeymap, useBindings, useActiveKeys, usePendingSequence, reactiveMatcherFromStore
  • @opentui/keymap/solid — KeymapProvider, useKeymap, useKeymapSelector, useBindings, reactiveMatcherFromSignal
  • @opentui/keymap/extras — helpers for cheat-sheet UIs (createBindingLookup, commandBindings, formatCommandBindings)

Usage

import { registerDefaultKeys } from "@opentui/keymap/addons"
import { createOpenTuiKeymap } from "@opentui/keymap/opentui"
import { KeymapProvider } from "@opentui/keymap/react"

const keymap = createOpenTuiKeymap(renderer)
registerDefaultKeys(keymap)

createRoot(renderer).render(
  <KeymapProvider keymap={keymap}>
    <App />
  </KeymapProvider>,
)

Create a keymap, install the addons you want, then pass the configured instance to your app. The React and Solid entrypoints consume a pre-created OpenTUI keymap through context.

Keymap is the core runtime. Pass feature factories to its constructor when you need graph snapshots or layer analyzers.

Adapters

Adapters implement a small KeymapHost interface (metadata, rootTarget, getFocusedTarget, getParentTarget, onKeyPress, onKeyRelease, onFocusChange, onTargetDestroy, ...). The HTML adapter normalizes DOM key names (Escape → escape, ArrowUp → up, Meta → super, Alt → meta) and tracks targets via MutationObserver. The OpenTUI adapter hooks CliRenderer keypress, keyrelease, focus, destroy, target destroy, and raw input events.

Formatting Keys

Use keymap.formatKey when displaying raw binding strings. It runs them through the keymap's parsers and tokens before stringifying.

keymap.formatKey("<leader>s", { separator: " " }) // "space s"
keymap.formatKey("<leader>s", { preferDisplay: true }) // "<leader>s"

Re-entry

Runtime/data re-entry is supported during dispatch: command handlers, intercepts, and pending-sequence listeners may read or write runtime data and pending-sequence state.

Structural re-entry is not supported. Do not register or unregister layers, tokens, parsers, or resolvers while a dispatch is in flight.

Installation

bun install @opentui/keymap

Development

bun run build
bun run test
bun src/keymap-benchmark.ts

The HTML demo lives in the docs app at /demos/keymap-html/ under packages/web.

版本与发布记录

@androidtui/keymap 共发布 29 个版本,首次发布于 2026/08/09;过去 12 个月发布了 29 个版本。

版本发布日期距今
0.8.0latest2026/10/308 天前
0.7.02026/10/308 天前
0.6.82026/10/2415 天前
0.6.62026/10/1524 天前
0.6.52026/10/1424 天前
0.6.42026/10/1425 天前

其他发布通道:future → 0.5.12-android.1。

关于 @androidtui/keymap 的常见问题

@androidtui/keymap 是什么?

Keybindings and command sequences for OpenTUI applications on Android and Termux

如何安装 @androidtui/keymap?

在项目目录运行 npm install @androidtui/keymap,或使用 pnpm add / yarn add / bun add。

@androidtui/keymap 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@androidtui/keymap 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@androidtui/keymap 可以商用吗?

@androidtui/keymap 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@androidtui/keymap 的下载量有多少?

根据 npm 官方下载统计,@androidtui/keymap 在 2026/10/06 – 2026/11/04 这 30 天内被下载 1,888 次,日均约 63 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@androidtui/keymap 还在维护吗?

最新版本 0.8.0 发布于 2026/10/30(8 天前);至今共发布 29 个版本,其中过去 12 个月发布了 29 个;首次发布于 2026/08/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。