关于 karabiner
此技能通过 GokuRakuJoudo 配置 macOS 键盘映射,以简洁的 EDN 规则编译生成 Karabiner-Elements JSON。适合需要定义按键映射、条件快捷键和分层操作的用户。
主要功能
- 01将 EDN 规则编译为 Karabiner JSON
- 02支持修饰键与条件按键映射
- 03配置 Simlayer 和轻按/长按层
- 04复用别名与 Shell 命令模板
- 05使用 Goku 监视配置及服务
适用场景
- 01将 Caps Lock 映射为 Escape
- 02设置应用专属键盘快捷键
- 03用 Space 创建导航层
- 04通过按键组合运行 Shell 命令
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ johnlindquist/ claude --skill karabiner安装后在 Claude Code 中输入/karabiner确认已加载。
附带文件edn-reference.md
详细介绍
Karabiner(通过 Goku)
使用 GokuRakuJoudo 配置 macOS 键盘重映射 - 一个基于 EDN 的 DSL,可编译为 Karabiner-Elements JSON。
为什么选择 Goku?
Karabiner 的原生 JSON 非常冗长(20,000+ 行)。Goku 的 EDN 格式简洁 10-50 倍:
;; Goku: 1 line
[:caps_lock :escape]
;; Karabiner JSON: ~30 lines
前置条件
# Install Goku
brew install yqrashawn/goku/goku
# Start as service (watches ~/.config/karabiner.edn)
brew services start goku
# Or run once manually
goku
配置位置:~/.config/karabiner.edn
日志:~/Library/Logs/goku.log
EDN 语法快速参考
基本结构
{:main [{:des "Rule description"
:rules [[:from :to]
[:from2 :to2]]}]}
键码
所有按键都使用关键字语法::a、:1、:f19、:spacebar、:return_or_enter
查找键码:
- 运行 Karabiner-EventViewer.app
- 参见 keys_info.clj
修饰键语法
| 符号 | 修饰键 | 示例 |
|---|---|---|
!C | 左 Command | :!Ca = Cmd+A |
!T | 左 Control | :!Ta = Ctrl+A |
!O | 左 Option | :!Oa = Opt+A |
!S | 左 Shift | :!Sa = Shift+A |
!Q | 右 Command | :!Qa |
!W | 右 Control | :!Wa |
!E | 右 Option | :!Ea |
!R | 右 Shift | :!Ra |
!F | Fn | :!Fa |
!P | Caps Lock | :!Pa |
!! | Hyper(Cmd+Ctrl+Opt+Shift) | :!!a |
## | 可选任意修饰键 | :##a |
组合修饰键::!CTSa = Cmd+Ctrl+Shift+A
规则格式
[:from :to] ;; Basic
[:from :to :condition] ;; With condition
[:from :to :condition {:alone :x}] ;; With options
多按键
;; Press sequence
[:a [:1 :2 :3]] ;; a -> types 1, 2, 3
;; Simultaneous press (from)
[[:j :k] :escape] ;; j+k together -> escape
Shell 命令
[:!!1 "open -a Safari"] ;; Hyper+1 runs shell command
条件
应用条件
{:applications {:chrome ["^com\\.google\\.Chrome$"]
:code ["com.microsoft.VSCode"]}
:main [{:des "Chrome only"
:rules [[:a :b :chrome]]}]}
设备条件
{:devices {:hhkb [{:vendor_id 1278 :product_id 51966}]}
:main [{:rules [[:a :b :hhkb]]}]}
输入源条件
{:input-sources {:us {:input_source_id "com.apple.keylayout.US"}}
:main [{:rules [[:a :b :us]]}]}
组合与取反条件
[:a :b [:chrome :hhkb]] ;; Both conditions
[:a :b [:!chrome]] ;; NOT in Chrome
层
Simlayers(推荐)
快速、基于同时按键的层 - 最适合打字速度:
{:simlayers {:w-mode {:key :w}} ;; Hold W activates layer
:main [{:des "w-mode shortcuts"
:rules [:w-mode ;; Apply to this layer
[:e "open -a Finder"] ;; W+E opens Finder
[:r "open -a Safari"] ;; W+R opens Safari
]}]}
Simlayer 选项:
{:simlayers {:w-mode {:key :w
:modi {:mandatory [:left_control]}}}} ;; Ctrl+W activates
标准层(轻触/按住)
轻触与按住时行为不同:
{:layers {:caps-mode {:key :caps_lock
:alone {:key :escape}}}} ;; Tap=Esc, Hold=layer
手动层变量
;; Set variable on keydown, clear on keyup
[:w ["w-mode" 1] nil {:afterup ["w-mode" 0] :alone :w}]
;; Use the variable as condition
[:e :!Ce ["w-mode" 1]] ;; W+E -> Cmd+E (only when w-mode=1)
模板
可复用的 shell 命令模式:
{:templates {:open "open -a '%s'"
:launch "/path/to/script.sh %s"
:alfred "osascript -e 'tell application \"Alfred\" to run trigger \"%s\"'"}
:main [{:rules [[:!!1 [:open "Safari"]] ;; %s replaced with Safari
[:!!2 [:launch "arg1"]]]}]}
预定义别名
:froms(输入按键)
{:froms {:delete {:key :delete_or_backspace}
:return {:key :return_or_enter}
:mouse1 {:pkey :button1}}}
:tos(输出操作)
{:tos {:spotlight {:key :spacebar :modi :command}
:paste {:key :v :modi :command}
:shift-click {:pkey :button1 :modi :left_shift}}}
自定义修饰键集
{:modifiers {:hyper [:command :shift :control :option]
:meh [:shift :control :option]}}
高级选项
规则中的第四个位置:
[:from :to :condition {
:alone :key ;; to_if_alone
:held :key ;; to_if_held_down
:afterup :key ;; to_after_key_up
:delayed {:invoked :x :canceled :y}
:params {:alone_timeout 200}
}]
配置文件设置
{:profiles {:Default {:default true
:sim 250 ;; Simultaneous threshold (ms)
:delay 500 ;; Delayed action time (ms)
:alone 1000 ;; to_if_alone timeout (ms)
:held 500}}} ;; Held threshold (ms)
常见工作流
添加一个简单的重映射
{:main [{:des "Caps to Escape"
:rules [[:caps_lock :escape]]}]}
添加应用专属快捷键
{:applications {:chrome ["com.google.Chrome"]}
:main [{:des "Chrome shortcuts"
:rules [[:!Cl :!Ct :chrome]]}]} ;; Cmd+L -> Cmd+T in Chrome
创建一个层
{:simlayers {:nav {:key :spacebar}}
:main [{:des "Navigation layer"
:rules [:nav
[:h :left_arrow]
[:j :down_arrow]
[:k :up_arrow]
[:l :right_arrow]]}]}
Hyper 键设置
{:main [{:des "Caps Lock to Hyper"
:rules [[:caps_lock :!CTOSleft_shift nil {:alone :escape}]]}]}
CLI 命令
goku # Compile karabiner.edn once
gokuw # Watch mode (compile on change)
goku -h # Help
# Service management
brew services start goku # Start watch service
brew services stop goku # Stop service
brew services restart goku # Restart after changes
# Check logs
tail -f ~/Library/Logs/goku.log
验证与调试
- 检查语法:Goku 会报告 EDN 解析错误
- 查看输出:
~/.config/karabiner/karabiner.json - 使用 EventViewer:Karabiner-EventViewer.app 显示按键事件
- 检查日志:
tail ~/Library/Logs/goku.log
最佳实践
- 对经常使用的快捷键使用 simlayers(比按住层更快)
- 按用途对规则分组,并使用描述性的
:des标签 - 对重复的 shell 命令模式使用模板
- 在
:froms/:tos中定义别名,用于复杂的按键定义 - 增量测试 - 添加一条规则,验证,重复
- 在重大更改之前备份配置
资源
评论
支持 Markdown