Update 2020: use this script here: eRCaGuy_dotfiles...touchpad_toggle.sh. It handles X11 and Wayland window servers, and versions of Ubuntu including 14.04, 16.04, 18.04, 20.04, and even 22.04.
See also my Ubuntu 22.04 answer here.
Original answer:
Tested and works on Ubuntu 14.04, 16.04, and 18.04.
Do like @Almas Dusal's answer, which does like @negusp's answer, except use my modified script instead since it has 3 additional benefits (UPDATE: benefits 1 and 3 below were added 12 Sept. 2019):
- It searches the output of
xinput
for both Touchpad
AND TouchPad
, since some systems have the P capitalized while others do not. This makes my script compatible with more systems (hopefully ALL systems).
- It adds the 2-second auto-closing popup windows via
zenity
in order to show whether your touchpad is now ENABLED or DISABLED. See screenshots of these popup windows below.
- It properly fixes your scroll wheel speed, enabling
imwheel
(which fixes your slow scroll wheel problem in Ubuntu/Linux when using a mouse scroll wheel) when you are using a mouse, while disabling imwheel
when using a touchpad, since imwheel
otherwise makes the touchpad scrolling really glitchy when enabled. Be sure to uncomment the respective lines to make this work. Ex: # imwheel -b "4 5"
and # killall imwheel
.
#!/bin/bash
# GS_toggle_touchpad.sh
# - toggle the touchpad on and off
# Gabriel Staples
# Written: 2 Apr. 2018
# Updated: 12 Sept. 2019
# References:
# - https://askubuntu.com/a/874865/327339
# - https://askubuntu.com/questions/844151/enable-disable-touchpad/1109515#1109515 <-- my own ans!
# Search for id number of "TouchPad" OR "Touchpad" in `xinput` list; manually type `xinput` to see all your devices
read TouchPadDeviceId <<< $( xinput | sed -nre '/TouchPad|Touchpad/s/.*id=([0-9]*).*/\1/p' )
state=$( xinput list-props "$TouchPadDeviceId" | grep "Device Enabled" | grep -o "[01]$" )
echo "TouchPadDeviceId = $TouchPadDeviceId"
echo "state = $state"
if [ "$state" -eq '1' ];then
xinput --disable "$TouchPadDeviceId"
zenity --info --text "Touchpad DISABLED" --timeout=2
# Next line required only if using imwheel to fix slow scroll speed in Chrome, for ex; see here:
# https://askubuntu.com/a/991680/327339
# imwheel -b "4 5"
else
xinput --enable "$TouchPadDeviceId"
zenity --info --text "Touchpad ENABLED" --timeout=2
# Next line required only if using imwheel to fix slow scroll speed in Chrome, for ex; see here:
# https://askubuntu.com/a/991680/327339
# killall imwheel
fi
Note that the imwheel thing is only required if you are following this answer to fix your scroll wheel speed too: Permanently fix Chrome scroll speed
Now, assign it a shortcut key of Ctrl + Alt + P (or whatever you choose [the 'P' stands for touch'P'ad in my case]). My addition to the script above is the zenity
self-closing popup window part, which I describe more in my answer here (https://superuser.com/a/1310142/425838).
Now, when I use the shortcut above, I see this popup window, which auto-closes after 2 seconds:

And if I press it again I get this popup window, which auto-closes after 2 seconds:

Perfect!
As an added bonus, if you are a heavy mouse user, just add the command which calls your script above to your startup programs in Ubuntu so it runs and disables your track pad every time your computer starts up! I have this set so I can quit bumping the track pad with the base of my hand and deleting stuff accidentally all the time! Now, when my hand bumps the track pad, nothing happens because it's disabled. :)
Note: this all works great in Ubuntu 18.04 as well as older versions. I've tested it in Ubuntu 18.04 and Ubuntu 14.04. Here's a screenshot from the shortcut settings window in 18.04:

Related:
- UPDATE: Get the latest version of this script from my eRCaGuy_dotfiles project here!
- Permanently fix Chrome scroll speed