Turn Laptop Trackpad Off and On by Sensing USB Mouse Plugged In or Not on Linux

So I am working on making my laptop even more enjoyable to use. I originally wrote a script that would require me to decide whether or not my trackpad was turned on or off. You can get my script to turn trackpad on and off in ubuntu linux here

I wanted to make it more automated and be able to turn it into a daemon on startup. You can find a discussion on how to run a script at startup here.

Below is what seems to be working for me and detecting my USB mouse.

Script to Detect Mouse and Turn On/Off Trackpad
#!/usr/bin/env bash ## Get the touchpad id. The -P means perl regular expressions (for \K) ## the -i makes it case insensitive (better portability) and the -o ## means print only the matched portion. The \K discards anything matched ## before it so this command will print the numeric id only. TID=$(xinput list | grep -iPo 'touchpad.*id=\K\d+') ## Run every second while : do ## Disable the touchpad if there is a mouse connected ## and enable it if there is none. xinput list | grep -iq mouse && xinput disable "$TID" || xinput enable "$T$ ## wait one second to avoid spamming your CPU sleep 1 done

// //