Friday, November 19, 2021

Ubuntu: Add a Keyboard Shortcut to Toggle the Keyboard Touch Pad

In recent years, I've been using laptops that have touch/mouse pads, just under the space-bar. While the mouse pad can be convenient when a mouse not not at hand, it can also become a burden, as they can be very sensitive, such that a slight contact from your palm with send the cursor to another part of the screen, or to an App that you don't indent to type into.

On occasion, I like to use the touch pad, so I don't want to turn it off entirely. I'd like to be able to switch it on and off instead. This can be done using a keyboard shortcut that calls on a program that does the touch-pad toggling.

Here is the code that should work to toggle most laptop based touch-pads...

toggle_touchpad.sh

#!/bin/bash

# Use device number matching touchpad
tpID=`xinput list | grep Touchpad | perl -pe 's/.*\s+id\=(\d+)\s+.*/$1/'`

if [[ $(xinput list $tpID | grep -Ec "disabled") -eq 1 ]]; then
    xinput enable $tpID
    DISPLAY=:0 notify-send --urgency=low --icon=/usr/share/icons/gnome/256x256/status/user-available.png "Touchpad enabled: $tpID"
else
    xinput disable $tpID
    DISPLAY=:0 notify-send --urgency=low --icon=/usr/share/icons/gnome/256x256/status/user-busy.png "Touchpad disabled: $tpID"
fi

exit 0

This script finds the id of the touch-pad via the xinput program. Then it uses the xinput program again to determine the state of the touch-pad (Enabled or Disabled), and then uses the xinput program one more time to toggle the state of the touch-pad.

I then associate this program to Alt+M, using the Ubuntu Settings dialog...

Use Settings->search to find "Keyboard Shortcuts".


 

Then press the "+" plus at the bottom of the list of Shortcuts to add a new Shortcut.

 

 

You are then, presented with a dialog box, for defining the new short cut. Fill it in as shown...

 

 

I hope this works for you!

Let me know either way!