使用Karabiner对键盘做了映射。在公司使用外接键盘,回家使用Mac自带键盘,所以做了两个Profile。但每天拔掉或插入键盘后,都要手动切换Profile,有点变麻烦。在网上找了一下,可以使用一个叫Hammerspoon的软件,再写lua脚本检测外设的插入和去除,来控制软件的执行。
安装使用说明:
http://www.hammerspoon.org/go/
本文参考:
http://caitengwei.com/blog/2016/01/new-toys-in-new-year/
https://github.com/tekezo/Karabiner/issues/354
Karabiner命令行使用方法:
https://pqrs.org/osx/karabiner/document.html.en
自己的脚本如下:
--------------------------- 脚本 start --------------------------
local usbWatcher = nilfunction usbDeviceCallback(data)
-- hs.alert.show(data.productID)
-- 这里使用的productID进行设备确认的,网上的代码是使用productName。
-- 但我的IKBC87键键盘没有productName,所以使用了productID进行设备确认的
if (data["productID"] == 16450) then
if (data["eventType"] == "added") then
hs.alert.show("IKBC键盘插进来了˚¬˚")
hs.execute('/Applications/Karabiner.app/Contents/Library/bin/karabiner select 1')
elseif (data["eventType"] == "removed") then
hs.alert.show("IKBC键盘插拔出来了˚¬˚")
hs.execute('/Applications/Karabiner.app/Contents/Library/bin/karabiner select 0')
end
end
end
usbWatcher = hs.usb.watcher.new(usbDeviceCallback)
usbWatcher:start()
--------------------------- 脚本 end --------------------------
这里使用的productID进行设备确认的,网上的代码是使用productName,但我的IKBC87键键盘没有productName,所以使用了productID进行设备确认的。其它的table(data是lua中一种table数据类型,就点像son)属性如下:
- productName - A string containing the name of the device
- vendorName - A string containing the name of the device vendor
- vendorID - A number containing the Vendor ID of the device
- productID - A number containing the Product ID of the device