Skip to content

多键序列 ​

boundSequence 要求按顺序在超时内按下多个键,类似 vim 的 d d。第一个键不会立即触发,引擎进入待续状态并等待后续键;任一步超时或按错键,序列作废。

序列在图层内跨元素优先于普通绑定:即使同一图层中另一个元素也用普通绑定绑定了序列的首键或后续键,序列仍会先接管这个键,普通绑定不会额外触发。结果与元素的 applyElement 顺序无关。

前置知识 ​

文章为什么需要
基本绑定序列是绑定的一种形式
绑定归属与所有者栈序列的归属与 boundKeyboard 相同

签名 ​

ts
boundSequence(keys: string[], handler: KeyHandler, options?: SequenceOptions): () => void
boundSequence(sequenceActionId: string, options?: SequenceOptions): () => void
boundSequence(keys: string[], sequenceActionId: string, options?: SequenceOptions): () => void
  • keys {string[]} 依次按下的键,至少两个;显式键形式必需,预设键形式不使用。
  • handler {() => void} 完整匹配后执行。
  • sequenceActionId {string} 已注册的 SequenceOperationEntry ID;它的 action 作为回调,keys / timeout 作为默认值。见快捷键与动作。
  • options {SequenceOptions} 在 BoundKeyboardOptions 上增加 timeout 与 exclusive。
  • 返回:解绑函数。
tsx
const { boundSequence } = useKeyboard()

useEffect(
  () => boundSequence(['d', 'd'], () => deleteItem()),
  [boundSequence],
)

// 使用已注册的序列动作
boundSequence('goto-top')
boundSequence(['ctrl+g', 'g'], 'goto-top', { timeout: 800 })

boundSequence(['a'], ...) 会抛错:requires at least 2 keys in the sequence。未注册的 sequenceActionId 会抛错:Action "<id>" is not registered.

选项 ​

选项类型作用默认值
timeoutnumber相邻按键的最大间隔(ms)500
exclusiveboolean按错键时静默吞掉并继续等待;false 则取消序列并放行该键false
when() => boolean | string条件—
modestring限定模式—
focusIdstring | FocusRef绑定到焦点目标—
elementIdstring绑定到图层元素自动注入
stopsWorkingAfterLayerAppearingboolean图层出现时休眠false
tsx
boundSequence(['g', 'g'], () => jumpToTop(), { timeout: 700 })
boundSequence(['w', 'w'], () => lock(), { exclusive: true })

times、once、observer 虽然出现在 SequenceOptions 类型中,但序列匹配不使用它们,只对 boundKeyboard 生效。

查询待续状态 ​

ts
currentScreenHasSequenceWaiting(sync?)
thereGlobalQueueWaiting(sync?)
  • currentScreenHasSequenceWaiting 读取调用方自身作用域:页面组件读到页面的序列,图层或模态元素读到该图层的序列。
  • thereGlobalQueueWaiting 对应 globalSequence。
  • 两者都接受可选的 sync 回调,引擎在每次按键后调用它,便于状态栏重新渲染。
tsx
const { currentScreenHasSequenceWaiting } = useKeyboard()
const [, setTick] = useState(0)
const sync = useCallback(() => setTick((n) => n + 1), [])

const waiting = currentScreenHasSequenceWaiting(sync)

它们只报告状态,不消费按键。

边界 ​

  • 每匹配一个键都会重置 timeout。
  • exclusive: false 时,按错的键会取消序列并继续走自己的绑定;true 时被吞掉。
  • 序列与映射键共享同一套归属和焦点规则。
  • 组件卸载前返回解绑函数,避免残留待续序列。

基于 MIT 许可证发布