Dragons Hobbies Forum

General Category => Blogs => Topic started by: Dragon on April 28, 2020, 21:15:27

Title: Automatically Enable and Disable Touchpad on Ubuntu
Post by: Dragon on April 28, 2020, 21:15:27
Since I've been using Ubuntu on my personal laptop, I often have a USB mouse plugged in and the touchpad disabled so that I'm not bumping it while I type. Most of the time it's not a big deal, but recently I've been switching the USB mouse from one laptop to the another. On Windows, the laptop was automatically detecting whether or not the USB mouse was plugged in, and it would enable and disable the touchpad automatically, but my Ubuntu laptop wasn't doing that. Switching the touchpad off when I had the USB mouse plugged in wasn't too bad - just going to settings and toggling the switch for the touchpad. However, if the USB mouse was not plugged in AND the touchpad was not enabled, I couldn't toggle the switch because I couldn't tab over to where the switch was in the GUI.

I decided to put together a script to make it easier for me to toggle the switch, initially just by having a command line script that I could run on the terminal. Quickly searching around Google, I found "synclient TouchpadOff=0" was the key command I needed. I found that when my USB mouse was plugged in, there was a file called "hiddev0" in my /dev/usb directory. When the USB dongle was pulled out, that would disappear. Here's the script that I eventually wrote.

Code: [Select]
# This script is to automatically enable and disable the touchpad.
USB=`ls /dev/usb 2>/dev/null`
if [[ "$USB" != "hiddev0" ]]; then
# echo "Touchpad Enabled - USB mouse was not detected"
synclient TouchpadOff=0
# echo "Touchpad Disabled - USB mouse was detected at $USB"
synclient TouchpadOff=1

With my initial script saved in a file called automaticMouse.sh (chmod +x), I was able to run the script manually, but decided to add it to my .bashrc file so that it would run automatically when I logged in. That worked great when I opened the terminal. Unfortunately, if I logged out of the GUI, unplugged the USB mouse, and logged in again, the script in my .bashrc file wasn't getting called. It wasn't until I hit CTRL+ALT+T to open the Terminal that the script would run. That was a little better but still not what I was going for. Startup Applications was where I needed to add it in order for the GUI to load it.

I was also a little curious about what the Startup Applications GUI actually create. I found this.
Code: [Select]
-rw-rw-r-- 1 dragon dragon 285 Apr 28 20:08 .config/autostart/automaticMouse.sh.desktop
Naturally a bit curious, I had a look inside. 

Code: [Select]
dragon@vaio:~$ cat .config/autostart/automaticMouse.sh.desktop
[Desktop Entry]
Comment[en_US]=Bash script to enable and disable touchpad
Comment=Bash script to enable and disable touchpad

That file was automatically generated by the Startup Applications GUI, but I just thought it might be useful to have this in the future.
Title: Re: Automatically Enable and Disable Touchpad on Ubuntu
Post by: Dragon on May 20, 2020, 16:37:31
Problem discovered: If I LOCK my laptop and then unplug my USB mouse, the automaticMouse.sh script doesn't get called when I unlock the PC, until I open a Terminal window. Not the worst, but it's something to take into consideration.