Turning Your Touchpad On or Off
Many laptops have a special key combination to enable or disable the touchpad. But what if yours doesn't? Luckily, Linux has built-in tools for touchpad control.
This guide will show you how to disable and enable the touchpad from the command line on X11 environments. We'll create a handy script for quick toggling and explore assigning a keyboard shortcut for even faster control.
The X Windows System implements a helper utility known as xinput. xinput lets us configure the input devices by querying the identifier of a device to manipulate the available settings for a device.
Most distributions don’t have this installed by default. However, we can easily install it from the official package repository under the canonical name xinput.
Verify the used version is like this or newer:
$ xinput --version
xinput version 1.6.3
XI version on server: 2.4
Now, we’re ready to manipulate the touchpad settings from the command line using xinput.
2.1. Getting the Touchpad ID
X11 assigns an identifier to every attached input device. We can list the input devices using the –list option:
$ xinput --list
⎡ Virtual core pointer id=2 [master pointer (3)]
⎜ ↳ Virtual core XTEST pointer id=4 [slave pointer (2)]
⎜ ↳ Elan Touchpad id=11 [slave pointer (2)]
⎜ ↳ Elan TrackPoint id=12 [slave pointer (2)]
⎜ ↳ ELAN Touchscreen id=9 [slave pointer (2)]
⎣ Virtual core keyboard id=3 [master keyboard (2)]
↳ Virtual core XTEST keyboard id=5 [slave keyboard (3)]
↳ Power Button id=6 [slave keyboard (3)]
In our case, we are interested in the “Elan Touchpad” device with its ID set to “11“. We’ll use this ID to query the available settings for the device:
Here, we can see that the device is currently enabled because the “187” switch is set to 1.
2.2. Toggle Enable/Disable Touchpad
In the previous section, we got the ID for our device and the switch ID to toggle the device state. Let’s use the switch ID to disable the device:
Let’s break it down:
set-prop sets a property for an input device
11 is the identifier for the touchpad device
187 is the switch identifier for enabling and disabling the device
0 specifies the disable option for the switch
Now, if we try to use the touchpad device, it won’t work. Let’s re-enable the device by setting the switch to 1:
Alternatively, we can also use the –enable and –disable options for the same purpose:
Similarly, let’s enable the device:
It gets a bit tedious if we have to do this multiple times. For that reason, we can create a script that can automate this process for us.
2.3. Automating With a Bash Script
Let’s create a script to search for the touchpad device and toggle its state accordingly:
The first line reads the touchpad identifier into the TPdevice variable by parsing the output of xinput. The second line greps the state of the device, which is either 0 or 1. Finally, we compare the state against “1” to either disable or enable the device.
For enhancements, we also send a notification to the system notification daemon that will provide us with visual feedback. Moreover, we can use zenity if we don’t have a notification daemon installed.
2.4. Quick Touchpad Toggle With a Shortcut
Now, if we want to take this script a step further, we can assign a keyboard shortcut to invoke the script. It will mimic the behavior of laptops that has this built-in functionality.
Goto Menu → Settings → click on Keyboard on the top bar you have Application Shortcuts, click the tab, now press “Add” and add the name of you script in Step 2.3 → press the shortcut you want to use, example SHIFT + ALT + F9 → Done. No worries you can later “edit” you shortcut.
Done.