public
Last active

Toggle touchpad on/off (python/linux)

  • Download Gist
toggle_touchpad.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/usr/bin/python2.7
import subprocess
 
def touchpad_currently_off():
all_states = subprocess.check_output(['synclient']).split('\n')
off_state = next((s for s in all_states if 'touchpadoff' in s.lower()), None)
return '1' in off_state
 
def main():
if touchpad_currently_off():
subprocess.check_call(['synclient', 'touchpadoff=0'])
else:
subprocess.check_call(['synclient', 'touchpadoff=1'])
 
if __name__ == '__main__':
main()

File "./toggle_touchpad.py", line 5
all_states = subprocess.check_output(['synclient']).split('\n')
^
IndentationError: expected an indented block
$

why is this happening in my system/pc

but this is working without python simply bash

Get the id number of the touchpad.
tp_id=xinput list | grep -i touchpad | awk '{ print $6 }' | sed 's/id=//'

Find out whether the touchpad is enabled or not.
tp_enabled=xinput list-props $tp_id | grep Device\ Enabled | awk '{ print $4 }'

if [ $tp_enabled = 0 ]
then
# The touchpad is currently disabled, so turn it on.
xinput set-prop $tp_id "Device Enabled" 1
echo "Touchpad now on."
elif [ $tp_enabled = 1 ]
then
# The touchpad is currently enabled, so turn it off.
xinput set-prop $tp_id "Device Enabled" 0
echo "Touchpad now off."
else
echo "tp_toggle: Could not get touchpad status from xinput."
exit 1
fi

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.