Mac 键盘映射hidutil+LaunchAgents

2,095 阅读3分钟

前言 公司的带有数字小键盘的妙控键盘不知道哪一次进水,导致⬆️和4,5,6 按钮不能使用了,之前一直使用的是Karabiner在软件级别重新映射key,但是在更新macOS Hign sierra之后就不能使用了,在作者做出兼容后,在我的电脑上又出现和锁屏组合键冲突(control + shift + ˆ),并且Karabiner karabiner_grabber进程的CPU使用率太高,所以还是自己用hidutil实现键盘的映射需求吧

核心脚本语句:

直接使用终端运行 hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000052,"HIDKeyboardModifierMappingDst":0x700000058}]}' 命令很语义化,这里解释一下0x700000052是Keypad_Enter键(小键盘的enter)的唯一编码值,更多的usage IDs

检查映射状态hidutil property --get "UserKeyMapping"

这里有个坑,在重启后映射的key值就会失效,每次开机都要手动去执行脚本也太low了吧,所以这里使用~/Library/LaunchAgents/.plist + shell自动完成工作,这里分开作讲解

1. 集成键盘映射的shell

FROM="\"HIDKeyboardModifierMappingSrc\""
TO="\"HIDKeyboardModifierMappingDst\""


UpArrow="0x700000052" 
Keypad_Enter="0x700000058"

Keypad_4="0x70000005C" #4
Keypad_5="0x70000005D" #5
Keypad_6="0x70000005E" #6
Keypad_clear="0x700000053" #clear
Keypad_eq="0x700000067"    # =
Keypad_slash="0x700000054" # /符号

hidutil property --matching '{"ProductID":0x220, "VendorID":0x5ac}' --set "{\"UserKeyMapping\":[
{$FROM: $Keypad_Enter,$TO: $UpArrow},
{$FROM: $Keypad_clear,$TO: $Keypad_4},
{$FROM: $Keypad_eq,$TO: $Keypad_5},
{$FROM: $Keypad_slash,$TO: $Keypad_6}
]}"

echo 更改键盘映射完成!

参数:

  1. ProductID, VerdorID: 设备参数,使用命令launchctl list可以查看: 我的键盘设备(Apple Keyboard):0x220 0x5ac
  2. 按键对应的键值,比如这里的上箭头UpArrow="0x700000052",都可以在usage IDs找到。

运行脚本:

sh  remappingKey.sh

执行成功的话会显示如下信息,这里30064771154对应0x700000052,不过是十六进制转换成了十进制

错误盘点:

稍微懂点shell的都看得懂的,这里记录一下遇到的错误:

  1. NSJSONSerialization Error Domain=NSCocoaErrorDomain Code=3840
  2. Unable to create property object for
  3. {NSDebugDescription=No string key for value in object around character 21.}
  4. {NSDebugDescription=Garbage at end.}

原因:

  1. shell语法错误,
  2. json格式不对,不要有莫名的空格,单词大小写
  3. "符号需要转译\",😂

2. Mac开机任务: ~/Library/LaunchAgents/.plist

  • ~/Library/LaunchAgents 针对当前用户的启动项目录
  • /Library/LaunchAgents 所有用户(管理员权限)
  • /System/Library/LaunchAgents 系统启动项
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.yuans.remapkeys</string>
    <key>ProgramArguments</key>
    <array>
        <string>zsh</string>
        <string>-c</string>
        <string>/Users/admin/Desktop/remappingKey.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

参数:

  • label:名称,最好是和plist文件名一样,我这里文件名为com.yuans.remapkeys.plist
  • ProgramArguments:启动文件配置 使用launchcontrol来检查开机启动plist的错误
  • RunAtLoad:加载后执行

运行:

launchctl load 启动plist运行
launchctl unload  卸载
launchctl list 查看所有启动任务
launchctl start 开始plist任务
launchctl error code 查看code对应的错误解释

load成功后,使用launchctl list 查看任务的status

ps: 一定要调试到status为0了,再重启电脑测试,不然成本太高了😂

  • 0:成功

其他code值可以使用命launchctl error令查看原因,列举我遇到的:

  • launchctl error 78, 查看code为78的错误描述:
    • 78: Function not implemented
    • s:路径问题
  • launchctl error 127:
    • 127: The specified service did not ship with the operating system
    • s:没有指定脚本运行环境
      1. 在shell第一行加入#!/usr/bin/env bash
      2. 在plist中配置<string>zsh</string> <string>-c</string>

借助可视化工具launchcontrol调试:

安装命令:brew cask install launchcontrol

UI界面:

本文git链接

More (mechanical) keyboards and Mac key remapping

Remapping Keys in macOS 10.12 Sierra

EffectiveMac