Skip to content

Instantly share code, notes, and snippets.

@coderiot
Created September 10, 2013 12:58
Show Gist options
  • Select an option

  • Save coderiot/6509030 to your computer and use it in GitHub Desktop.

Select an option

Save coderiot/6509030 to your computer and use it in GitHub Desktop.
bash script toggle touchpad with xinput.
#!/bin/bash
id=$(xinput | grep TouchPad | grep -E -o "id=[[:digit:]]+" | grep -E -o "[[:digit:]]+")
if [ "$id" = "" ]
then
echo "no touchpad found."
exit 0
fi
status=$(xinput list-props 10 | grep "Device Enabled" | cut -f3)
if [ $status -eq 0 ]
then
xinput set-prop $id "Device Enabled" 1
echo "enabling device $id"
else
xinput set-prop $id "Device Enabled" 0
echo "disabling device $id"
fi
Copy link
Copy Markdown

ghost commented Nov 28, 2021

Hello!

Thanks for creating this. However it appears to me that you have hardcoded your device number (10), so if anyone's touchpad is in any other number this will not work. Additionally (at least in my case), xinput will call them Touchpad and not TouchPad. Lastly, more than one pointing device under that category can be present, and your script will crash if it is the case, as it will try to pass 2 IDs separated by a new line whenever we use the ID (in your case it may not, as it is hardcoded).

The following modification will use the IDs captured by grep and loop through them to enable/disable all of them (also uses the string "Touchpad" instead of "TouchPad", but that is just for my own setup.

I am a noob and don't know how to submit merge requests so I will just paste it here:

#!/bin/bash

# List devices called anything "Touchpad" and get their numerical IDs
id=$(xinput | grep Touchpad | grep -E -o "id=[[:digit:]]+" | grep -E -o "[[:digit:]]+")

# Handle cases where no devices are found
if [ "$id" = "" ]
then
    echo "no touchpad found."
    exit 0
fi

# Loop through captured IDs and toggle their status
for i in $id;do
	status=$(xinput list-props "$i" | grep "Device Enabled" | cut -f3)
    	if [ $status -eq 0 ]
    	then
            xinput set-prop "$i" "Device Enabled" 1
            echo "enabling device $id"
    	else
            xinput set-prop "$i" "Device Enabled" 0
            echo "disabling device $id"
    fi
done

Thanks again, this helped me manage my touchpad in i3wm. For anyone who might be here for the same reason, save this script as, for example, touchpad-toggle.sh, and add this line to your i3 config file:

bindsym XF86TouchpadToggle exec /home/path/to/configfile/touchpad-toggle.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment