Skip to content

UI 热键 API

客户端 UI 脚本通过全局对象 Hotkeys(大写)注册/管理 UI 热键。

UI 热键只在当前 UI 上下文生效(按 storeId/runtimeContextKey 隔离)。

Quick Start

js
const id = Hotkeys.register({
  pattern: "CTRL+K",
  trigger: "press",
  consume: true,
  priority: 10
}, (payload) => {
  console.log("触发热键:", payload.pattern)
})

Hotkeys.unregister(id)
Hotkeys.clear()

API Reference

Hotkeys.register(options, callback?)

注册一条 UI 热键,返回热键 ID;注册失败返回 null

  • callbackoptions.on 二选一;若都提供,优先使用 options.on
  • 必须提供回调,否则注册失败。
参数类型说明
optionsobject热键选项,见下方字段表
callbackfunction触发回调(可选)

返回值:string | null

Hotkeys.unregister(id)

注销指定 ID 的热键。

参数类型说明
idstringregister 返回的热键 ID

返回值:boolean

Hotkeys.clear()

清理当前 UI 上下文注册的全部热键。

Hotkeys.list()

返回当前 UI 上下文已注册的热键 ID 列表。

返回值:string[]

options 字段参考

字段类型默认值说明
pattern / keysstring--热键模式,必填
triggerstring"press"press / release / hold
consumebooleantrue命中后是否阻断后续匹配
allowWhenTypingbooleanfalse输入组件聚焦时是否允许触发
priorityint0优先级(越大越先触发)
cooldownMs / cooldownint0冷却时间(毫秒)
sequenceIntervalMs / sequenceIntervalint320连招步进最大间隔(毫秒)
holdMs / holdintnull按住触发阈值(毫秒)
idstring自动生成可选自定义 ID
onfunctionnull回调函数(优先于第二参数 callback

pattern 语法

语法写法说明
连招"W, W"逗号分隔,依序按下
组合键"CTRL+K"+ 表示同一时间按下
按住"F@300"@ 后毫秒数,达到时触发
组合按住"SHIFT+RMB@600"支持组合 + 按住

回调 payload

回调收到对象结构:

字段类型说明
idstring热键 ID
contextstring固定为 "ui"
triggerstring"press" / "release" / "hold"
timenumber客户端触发时间戳(ms)
sourcestring固定为 "ui"
patternstring匹配到的原始 pattern
js
{
  id: "panel:hotkey:1",
  context: "ui",
  trigger: "press",
  time: 1700000000000,
  source: "ui",
  pattern: "CTRL+K"
}

常见问题

  1. UI 热键只在当前 UI 上下文生效,不会影响其他 UI。
  2. allowWhenTyping = false 时,TextField/TextArea/TagField 聚焦下不会触发。
  3. Hotkeys.clear() 仅清理当前 UI 上下文,不会删除服务端热键。