Settings
快捷键自定义
键盘作用域、默认快捷键表与自定义方式
快捷键自定义
WinCode 的快捷键系统基于作用域(Scope)模型,由 KeyboardTab 展示、keyboardStore 管理、useRegisterShortcut Hook 在各组件中注册。用户在设置页可直接按键重绑任意快捷键。

作用域(Scope)
快捷键只在对应的 active scope 中生效,作用域随界面动态 push/pop:
| Scope | 触发时机 |
|---|---|
global | 始终激活,根作用域 |
chat | 进入会话界面 |
chat.slashMenu | 斜杠命令菜单打开时(子作用域) |
welcome | 欢迎页激活 |
fileTree | 文件树聚焦 |
modal | 任意模态框打开时 |
// useKeyboardScope.ts
export function useKeyboardScope(scope: string, active: boolean = true) {
const pushScope = useKeyboardStore((s) => s.pushScope);
const popScope = useKeyboardStore((s) => s.popScope);
useEffect(() => {
if (active) {
pushScope(scope);
return () => popScope(scope);
}
}, [scope, active, pushScope, popScope]);
}当多个作用域同时激活时,优先级更高的 scope 内的绑定胜出;同 scope 内按 priority 字段比较。
默认快捷键表
从源码中提取的默认绑定:
| 作用域 | 描述 | 默认键 | 组件 |
|---|---|---|---|
| global | 打开设置 | cmd+, | Layout.tsx |
| global | 切换左侧边栏 | cmd+shift+s | Layout.tsx |
| global | 切换右侧资源面板 | cmd+shift+e | AppWorkspace.tsx |
| chat | 发送消息 | enter | MessageInput.tsx |
| chat | 聚焦输入框 | cmd+i | MessageInput.tsx |
| welcome | 发送消息 | enter | WelcomeInput.tsx |
| welcome | 聚焦输入框 | cmd+i | WelcomeInput.tsx |
| chat.slashMenu | 下一个命令 | arrowdown | SlashCommandMenu.tsx |
| chat.slashMenu | 上一个命令 | arrowup | SlashCommandMenu.tsx |
| chat.slashMenu | 选择命令 | enter | SlashCommandMenu.tsx |
| chat.slashMenu | 关闭菜单 | escape | SlashCommandMenu.tsx |
| fileTree | 关闭上下文菜单 | escape | FileTree.tsx |
自定义快捷键
KeyboardTab 按 scope 分组展示所有已注册绑定,每项右侧是 KeyboardShortcutInput 录入控件:
- 点击输入框进入录制模式(
setRecording(true)) - 按下目标键,Hook 将其写入
userOverrides并持久化到localStorage['keyboard-shortcuts-overrides'] - 下次 dispatch 时,
effectiveKey = userOverrides[id] ?? binding.key优先使用用户覆盖
右上角 恢复默认 按钮调用 resetToDefaults(),清空 localStorage 中的覆盖记录。
注册新快捷键
功能组件中使用 useRegisterShortcut:
useRegisterShortcut({
id: 'settings.open',
defaultKey: 'cmd+,',
scope: 'global',
description: '打开设置',
handler: toggleSettings,
});配置字段:id(唯一标识)、defaultKey(默认键)、scope、description(显示在设置页)、handler(回调)、priority(可选)、targetFilter(过滤触发元素)、when(条件)、preventDefault(默认 true)。
派发与优先级
Layout.tsx 在窗口挂载全局 keydown 监听,事件交给 keyboardStore.dispatch:
- 过滤:若
isRecording或event.isComposing(中文输入法)直接返回 - 匹配:检查 key / scope / targetFilter / when
- 排序:优先 scope 深度,再按 priority
- 命中首个后
preventDefault()并执行 handler