Application to run as a service that detects a given device and then runs a script when plugged-in and out, as an example the code in this repository will swich the keyboard layout and scroll direction when a certain device.
Example Usage
- Docking Station is plugged in
- DHS detects this and performs the following actions:
- Switches the keyboard layout to match the new keyboard being used
- Switches the scroll direction to match the new mouse being used
- Docking station is unplugged
- DHS detects this and performs the following actions:
- Reverts the keyboard layout to original layout
- Reverts the scroll direction to original scroll direction
An install script has been created, running this script will prompt for the device to detect and then to set-up the service.
Currently only MacOS Ventura is supported.
Download and further instructions can be found on the Releases.
DHS can run any script when detecting devices, for the sake of this repository it will change the keyboard and mouse scroll direction.
Changing Keyboard Layout and Scroll Direction
Change the the positional parameters for /usr/local/bin/SwitchKeyboard & /usr/local/bin/SwitchScrollDirection in CheckDeviceStatus.sh
Running custom scripts
To run custom scripts, change /usr/local/bin/SwitchKeyboard & /usr/local/bin/SwitchScrollDirection to the script(s) desired in CheckDeviceStatus.sh.
then
/usr/local/bin/SwitchKeyboard "British-PC"
/usr/local/bin/SwitchScrollDirection "Standard"
else
/usr/local/bin/SwitchKeyboard "British"
/usr/local/bin/SwitchScrollDirection "Natural"The code in the then block is when the device is plugged in, the code in the else block is when the device is unplugged
To kill the service run the following command:
kill -9 $(ps aux | grep '/usr/local/bin/CheckDeviceStatus' | awk '{print $2}' | head -n 1)The keyboard handler is built using Swift as the logic for TISSelectInputSource & TISCreateInputSourceList are closed source. The script takes one positional argument which is the keyboard layout.
Keyboard switching on Ventura requires the input source to already be configured in System Settings -> Keyboard -> Input Sources -> Edit -> + Source
Build from source:
swiftc -framework Carbon -framework Foundation ./Keyboard-Handler/src/SwitchKeyboard.swiftRequires Xcode tools
The scroll handler uses defaults to write to com.apple.swipescrolldirection and then uses activateSettings -u to activate the changes without requiring a restart.
The service is created using CheckDeviceStatus.sh -- the script will continiously run ioreg -p IOUSB every x seconds to check if any new devices have been plugged in, if a device has been plugged in it will check if this is the DEVICE_TO_CHECK then it will perform actions when plugged-in and unplugged.
The DEVICE_TO_CHECK can either be set as an env var or a positional argument
The service is best created by having an application that is auto-run on login, this is to avoid Apple's restrictions on services controlling the desktop interactively.
Set up an Automator Application:
Set the application to have Run Shell Script action with the following shell command:
/usr/local/bin/CheckDeviceStatus "<Device to Monitor>" &>/dev/null &After exporting, this will provide a .app, like the one found on the repo -- once this app has the required permissions it can run like any other application.
Provide DHS with Permissions:
System Preferences --> Security & Privacy --> Click the lock to make changes --> Add DHS to the list
Enable DHS on login:
System Preferences --> Users & Groups --> <Your User> --> Login Items --> Click the lock to make changes --> Add DHS to the list