The answer is written for Ubuntu 22.04 LTS (Jammy Jellyfish) that has libinput driver (module) instead of old synaptics driver that was in older Ubuntu versions.
Download and run script that auto disable touchpad on usb mouse plug in.
The script will work if you have one touchpad only!
You may have as many mice as you want.
If you need silent version of the script then remove all echo and notify-send commands (the script file lines).
FYI I am not a bash scripts' expert and I wrote the script for myself, I think the script may be optimized but for me it is enough.
FYI 2 If you have KDE you may find suggestions to install a synaptics driver (module) instead of libinput (default driver). In my case it does not help at all because KDE touchpad configuration has some errors when I use synaptics driver.
If you have non standard touchpad with word "Mouse" in its name then you need to modify the script by adding the name to FAKE_MCOUNT variable of the script to exclude the touchpad device from detection algorithm. For example I have ASUS laptop and ELAN touchpad that have "Mouse" word in its name, so the script need to know that it is not a mouse. You can see word ELAN in the script and how it is used. The script uses that regular expression do find needed usb mouse plug-in event.
# Execute these commands one by one.
print list of devices to check if you have a touchpad
with "Mouse" word in its name
unplug your actual mouse before executing the command
If you have such touchpad then you need to modify FAKE_MCOUNT variable of the script
xinput list
install curl utility to download a stuff
sudo apt install curl
install utility to show notifications
sudo apt install libnotify-bin
download and run the script
sudo curl -L https://gitlab.com/blog.awesomesoft/blog.awesomesoft/-/raw/master/src/linux/sh/off_touchpad_on_usb_mouse.sh -o ~/off_touchpad_on_usb_mouse.sh
sudo chmod a+rx ~/off_touchpad_on_usb_mouse.sh
~/off_touchpad_on_usb_mouse.sh
At the moment the script looks like
#!/bin/bash
The script listen mouse plug in event. And when mouse is pluged in then touchpad will be off.
The script works with one touchpad only!
The script works with USB mouse only (or mouse that emulate USB connection)!
The script works with any quantity of usb mice.
TID=xinput list | grep -Eo '\sTouchpad\s+id\=[0-9]{1,}' | grep -Eo '[0-9]{1,}'
MCOUNT=xinput list | grep -Eo '\sMouse\s+id\=[0-9]{1,}' | grep -c ^
Fake touchpad count
Add any fake touchpad to the expression, you can find it with "xinput list" command
e.g. ELAN is an ASUS touchpad that have "Mouse" word in its name
https://github.com/mishurov/linux_elan1200_touchpad
FAKE_MCOUNT=xinput list | grep -Eo 'ELAN.+\sMouse\s+id\=[0-9]{1,}' | grep -c ^
echo mouse count $MCOUNT
echo fake mouse count $FAKE_MCOUNT
MCOUNT=$((MCOUNT-FAKE_MCOUNT))
echo actual mouse count $MCOUNT
function inc_mouse_count {
MCOUNT=$((MCOUNT+1))
echo current mouse count $MCOUNT
}
function dec_mouse_count {
MCOUNT=$((MCOUNT-1))
echo current mouse count $MCOUNT
}
function touchpad_off {
if [ $MCOUNT -eq 1 ]
then
xinput --disable $TID
notify-send "Touchpad is OFF."
fi
}
function touchpad_on {
if [ $MCOUNT -lt 1 ]
then
xinput --enable $TID
notify-send "Touchpad is ON."
fi
}
if [ $MCOUNT -gt 0 ]
then
touchpad_off
else
touchpad_on
fi
RegExp explanation https://www.debuggex.com/r/4aJKR6XKjYFO1-1o
udevadm monitor -k | stdbuf -o0 grep -P '^KERNEL[' | stdbuf -o0 awk '{print $2, $3, $4}' | stdbuf -o0 grep -P '^(add|remove)\s/devices/[^/\s]+/[^/\s]+/[^/\s]+/usb\d+[^\s]+/mouse\d+\s(input)$' | stdbuf -o0 awk '{print $1}' | while IFS= read -r line;
do
if [ "$line" = "add" ]
then
inc_mouse_count
touchpad_off
elif [ "$line" = "remove" ]
then
dec_mouse_count
touchpad_on
fi
done