2021年6月

鼠标用的是MX2,支持Logi Flow,但是键盘是HHKB自己该的YDKB的蓝牙主控,现在想在俩台电脑之间切换鼠标的时候也能自动切换键盘。

想到一个简单的解决方案,就是在MX2切换到另一个设备的时候,当前机器的蓝牙会断开链接,那么我只要发现一旦鼠标断开链接同时也就断开键盘的链接,然后再另一台电脑上判断一旦发现鼠标链接进来了,就自动链接蓝牙键盘

需要先安装blueutil和bluetoothconnector

brew install bluetoothconnector
brew install blueutil

1.0 版本

基于这个逻辑写了一个简单的bash脚本,但是发现挂后台不是很方便

#!/bin/bash

ConnectHHKB(){
    check=`blueutil --connected|grep HHKB|wc|awk '{print$1}'`
    if [ $check == "0" ]
    then
        bluetoothconnector --connect fb-fd-74-d3-60-61
    fi
}

DisConnectHHKB(){
    check=`blueutil --connected|grep HHKB|wc|awk '{print$1}'`
    if [ $check == "1" ]
    then
        bluetoothconnector --disconnect fb-fd-74-d3-60-61
    fi
}
check(){
    check=`blueutil --connected|grep MX|wc|awk '{print$1}'`
    if [ $check == "0" ]
    then
        DisConnectHHKB
    else
        ConnectHHKB
    fi
}
while True
do
    check
    sleep 0.1
done

hammerspoon版本

因为YDKB断开后会自动链接另一个设备,我只要去执行一个disconnect就可以了
然后写一个hamm的init.lua

local watcher = hs.timer.new(1, function ()
    local m = hs.mouse.count()
    if m==2 then
        hs.execute("bash ~/.hammerspoon/disconnect.sh",true)
        print("mouse disconnect")
    end
    print(m)
    -- hs.alert.show(m)
end)
watcher:start()

实际disconnect.sh

check=$(blueutil --connected | grep HHKB | wc | awk '{print$1}')
if [ $check == "1" ]; then
    bluetoothconnector --disconnect fb-fd-74-d3-60-61
fi

参考资料

https://www.hammerspoon.org/docs/hs.html
https://www.ahonn.me/blog/how-to-implement-clipboard-history-with-hammerspoon
https://www.hammerspoon.org/docs/hs.timer.html
https://www.hammerspoon.org/go/#helloworld