Quickly turn touchpad on and off

Published: Wed, 06 Dec 2017
Modified: Tue, 26 Dec 2017

In: Linux

Tags: touchpad

Translations: bs

My laptop has a button to toggle the touchpad on and off. It's an XF86TouchpadToggle button and on its own it's useless. You need something which binds to it and makes it do things. Most desktop environments have an utility which listens to keyboard presses and then toggles the touchpad. Since I'm using a self-assembled environment I don't have anything like that.

Therefore I wrote a little abstraction script utilizing xinput. Xinput can print information about all X devices, but also change their options. Of course it can also disable a device and that's what we're going to use.

Little update: A lookbehind regex replaces the two "cut" calls.

:::bash
#!/usr/bin/bash

# worse way
# id=$(xinput | grep -i touchpad | cut -f 2 | cut -d "=" -f 2)
id=$(xinput | grep -i touchpad | grep -oP "(?<=id=)\d*")

if xinput list-props "$id" | grep -q "Device Enabled.*1$"; then
  xinput disable "$id"
  notify-send "Touchpad turned OFF" --icon=input-touchpad
else
  xinput enable "$id"
  notify-send "Touchpad turned ON" --icon=input-touchpad
fi

Pretty solution

I kept this script in a local bin directory and then bound the button via Openbox's settings to run the script. This worked, until I tried to run it in a different user's session. It didn't work because the script was in my HOME directory.

That's when I decided to package it neatly for my distribution. ArchLinux users can use this PKGBUILD. After you install it, an executable named toggle-touch should be available and then just bind a key to run it.