Author Topic: Automatically Enable and Disable Touchpad on Ubuntu  (Read 526 times)

Dragon

  • Administrator
  • Platinum Level
  • *****
  • Posts: 4553
  • Trogdor!
    • My Funagain Games Wishlist
Automatically Enable and Disable Touchpad on Ubuntu
« 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]
#!/bin/bash
# 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
else
# echo "Touchpad Disabled - USB mouse was detected at $USB"
synclient TouchpadOff=1
fi


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]
Type=Application
Exec=/home/dragon/automaticMouse.sh
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[en_US]=AutomaticMouse
Name=AutomaticMouse
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.
"Hello IT. Have you tried turning it off and on again? ... OK, well, the button on the side. Is it glowing?... Yeah, you need to turn it on. Err, the button turns it on. Yeah, you do know how a button works, don't you? No, not on clothes." - Roy (The IT Crowd)

Dragon

  • Administrator
  • Platinum Level
  • *****
  • Posts: 4553
  • Trogdor!
    • My Funagain Games Wishlist
Re: Automatically Enable and Disable Touchpad on Ubuntu
« Reply #1 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.
"Hello IT. Have you tried turning it off and on again? ... OK, well, the button on the side. Is it glowing?... Yeah, you need to turn it on. Err, the button turns it on. Yeah, you do know how a button works, don't you? No, not on clothes." - Roy (The IT Crowd)