How can I toggle the touchpad depending on whether a mouse is connected?
Okay, I've made an udev rule for it, and it is, like @terdon said, a much cleaner way
So, thanks to this guide, I created a "touchpad_toggle.rules" file in /etc/udev/rules.d/ (requires root access) and filled it with two lines:
SUBSYSTEM=="input", KERNEL=="mouse[0-9]*", ACTION=="add", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/username/.Xauthority", RUN+="/home/username/on.sh"
SUBSYSTEM=="input", KERNEL=="mouse[0-9]*", ACTION=="remove", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/username/.Xauthority", RUN+="/home/username/off.sh"
Don't forget to replace "username" with your username!
The contents of these on and off shell scripts are just castrated version of the script in my question. Example - off.sh:
#!/bin/bash
declare -i TID
TID=`xinput list | grep -Eo 'Touchpad\s*id\=[0-9]{1,2}' | grep -Eo '[0-9]{1,2}'`
xinput disable $TID
You'll have to use xinput enable $TID in the on.sh
And don't forget to add the script in my question (or the one @terdon suggested, but without a loop) to session autostart like he told you in his answer.
That is it, but I have to add one thing:
If you have a Synaptics touchpad (I have Elantech, so it's not suitable for me), you can replace your scripts (paths to which you write after RUN+=) with a simple command /usr/bin/synclient TouchpadOff=0
and 1 respectively
The basic script you need is simply:
#!/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 "$TID"
## wait one second to avoind spamming your CPU
sleep 1
done
The script above will toggle the trackpad depending on whether a mouse is connected. When launched, it will run for ever and will check for a mouse every second, disabling or enabling the touchpad accordingly.
Now, save the script as ~/touchpad.sh
, make it executable (chmod +x ~/touchpad.sh
) and add it to your GUI session startup programs. You have not specified which desktop environment you are using but since you mentioned lxtask
, I will assume you are using LXDE
. In any case, here are instructions for both LXDE
and Unity
:
Add the script to LXDE's autostart files
echo "@$HOME/touchpad.sh" >> ~/.config/lxsession/PROFILE/autostart file
Make sure you replace "PROFILE" with the actual name of your LXDE profile, you can find out what it is by running
ls ~/.config/lxsession/
.Add the script to Unity's autostart files
Open
Startup Applications
(search in the dashboard for "Startup")Click on "Add" and then paste the path to your script in the command field: