Quickly Toggle Your Touchpad in Linux


For me it is often frustrating trying to use my laptop’s touchpad to do anything that requires even the slightest bit of accuracy, so I use an external mouse whenever possible. Unfortunately, the placement of the touchpad means that I will invariably end up touching it with my palms while I type. Today I finally had enough and decided to take matters into my own hands. The following script will allow you to either toggle your touchpad based upon it’s current setting, or to explicitly turn your touchpad on or off (perhaps as an event to be fired when you plug in or unplug an external mouse). Without further ado:

# toggleTouchpad by Brendon Dugan
# Toggles a touchpad on or off depending on it's current state or CLI argument
#
# To configure, run the command 'xinput list' in terminal and identify your touch pad.
# Using the output of the above command, change the touchpadString variable to a substring
# of your touchpad's description that is unique to that device.
#
# To run, simply type 'toggleTouchpad' to toggle your touchpad on or off, or
# 'toggleTouchpad on' to explicitly turn your touchpad on, or
# 'toggleTouchpad off' to explicitly turn it off.
#
# Enjoy!
touchpadString="TouchPad"
touchpadID=$(xinput list | grep "$touchpadString" | awk -F " " '{print $6}' | awk -F "=" '{print $2}')
touchpadEnabled=$(xinput list-props $touchpadID | grep "Device Enabled" | awk -F ":" '{print $2}')

# Check for arguments on the command line
if test $# -eq 1
then
	# Change the argument to lowercase
	arg1=$(echo $1 | tr [:upper:] [:lower:])
	cliArg=1
else
	# There is no argument.
	cliArg=0
fi

if [ $cliArg -eq 1 ]
then
	# If there's an argument, check to see whether it is on, off, or junk
	if [ $arg1 = 'on' ]
	then
		# The argument was 'on', so turn the touchpad on
		xinput --set-prop $touchpadID "Device Enabled" 1
	elif [ $arg1 = 'off' ]
	then
		# The argument was 'off', so turn the touchpad off
		xinput --set-prop $touchpadID "Device Enabled" 0
	else
		# The argument was junk, so do nothing
		sleep 1
	fi
else
	# There was no argument, so just toggle the touchpad to the opposite
	# of the state it has now.
	if [ $touchpadEnabled -eq 1 ]
	then
		xinput --set-prop $touchpadID "Device Enabled" 0
	else
		xinput --set-prop $touchpadID "Device Enabled" 1
	fi
fi
About these ads
Leave a comment

6 Comments

  1. Jef Tobias

     /  November 28, 2010

    Would love to implement this, but not sure how to. Followed the directions and don’t understand the output of the xinput list command. I have a touchpad (Dell Inspiron) and it works in Kubuntu 10.04, but using that command yields this:
    ⎡ Virtual core pointer id=2 [master pointer (3)]
    ⎜ ↳ Virtual core XTEST pointer id=4 [slave pointer (2)]
    ⎜ ↳ KYE OPTICAL MOUSE id=10 [slave pointer (2)]
    ⎜ ↳ PS/2 Generic Mouse id=12 [slave pointer (2)]
    ⎜ ↳ Macintosh mouse button emulation id=13 [slave pointer (2)]
    ⎣ Virtual core keyboard id=3 [master keyboard (2)]
    ↳ Virtual core XTEST keyboard id=5 [slave keyboard (3)]
    ↳ Video Bus id=6 [slave keyboard (3)]
    ↳ Video Bus id=7 [slave keyboard (3)]
    ↳ Power Button id=8 [slave keyboard (3)]
    ↳ Sleep Button id=9 [slave keyboard (3)]
    ↳ AT Translated Set 2 keyboard id=11 [slave keyboard (3)]
    ↳ Dell WMI hotkeys id=14 [slave keyboard (3)]

    Would appreciate more help. Thanks.

    Reply
    • Brendon Dugan

       /  November 28, 2010

      It looks like your touchpad is being recognized as “PS/2 Generic Mouse”. If you change the line touchpadString="TouchPad" to touchpadString="Generic Mouse" you should have everything working! Make sure to check out my most recent post to help you set your touchpad to automatically disable when you attach your external mouse. Enjoy!

      Reply
  2. Jef Tobias

     /  November 28, 2010

    After changing the script, when I type “toggleTouchpad” into a console, I get “command not found.” I’ve done chmod to make it executable, but I’m still a newbie at this sort of thing…

    Reply
  3. Rostislav

     /  August 30, 2011

    $touchpadString might have spaces, so quotes needed around it in next line…

    touchpadID=$(xinput list | grep “$touchpadString” | awk -F ” ” ‘{print $6}’ | awk -F “=” ‘{print $2}’)

    Best regards,
    Rostislav

    Reply
  4. Elwood

     /  January 24, 2013

    Cool.

    I did have to change line 14 that gets the touchpadID to print field $7 instead of $6. It’s probably because “xinput list” describes my pad with four words, while most devices need three.

    But, here’s what I finally used:

    touchpadID=$(xinput list | grep “$touchpadString” | sed “s/.*id=//” | awk -F ” ” ‘{print $1}’)

    The sed just pitches everything through id=, which avoids the problem entirely.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.