插入外接键盘后,自动切换Karabiner的Profile

使用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 = nil
function 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值