Perform an action when bluetooth keyboard is connected or disconnected

bluetoothkeyboardscript

I am trying to write a script that can detect when a bluetooth keyboard is connected or disconnected from my mac and perform an action. The keyboard is a Logitech MX but I don't want my script to be dependent of that.

I can use bash, applescript, C, or any other language that can be called/binded to node.js

Best Answer

The app Tooth Fairy ($5.99) should be able to do this. Although it's designed for AirPods, it works just as well with keyboards and other Bluetooth devices. After adding a device, you can set scripts to run upon connect and disconnect, so you can call your own scripts to perform whatever action you'd like to take.

Tooth Fairy advanced settings menu