home > linux > scripts > linux-switch-touchpad-on-and-off

Linux Switch Touchpad On and Off

19 | 14 Jul 2011

It is very useful to switch a laptops touchpad off temporarily whilst working in command line, the following script should work across all Linux based systems.

#!/bin/bash
cd $HOME

function messageUser()
{
    if [ "$(which notify-send)" == "" ]; then
        echo "requires... libnotify-bin for messages..."
    elif [ "$1" == "on" ]; then
        notify-send -t 800 -i /home/mike/myicon.png "Touchpad enabled..." "mike reprogramed the calculator button :)"
    elif [ "$1" == "off" ]; then
        notify-send -t 800 -i /home/mike/myicon.png "Touchpad disabled..." "mike reprogramed the calculator button :)"
    fi
}

touchPadId=$(xinput list | grep -i Point | grep "PS/2" | cut -d "=" -f 2 | cut -b 1-2)
if [ "$touchPadId" == "" ]; then
    echo "Unable to identify device id..."
else
    enabledId=$(xinput --list-props $touchPadId | grep -i enabled | sed 's/.*(//' | sed 's/).*//')
    if [ -a ".mike-touchPadOnOff" ]; then
        rm -rfv ".mike-touchPadOnOff"
        xinput set-prop $touchPadId $enabledId 1
        messageUser "on"
    else
        touch ".mike-touchPadOnOff"
        xinput set-prop $touchPadId $enabledId 0
        messageUser "off"
    fi
fi

Script explained

to switch the touch pad on and off you need to set the enabled propety id on the device (we use xinput set-prop). The information required by set-prop is the device id, the enabled property id and the value 1 or 0 to enable and disable.

To get the device ID

xinput -list

This provides us a list of all input devices, to limit the devices down to mouse / touchpad devices we add the grep for point. To further limit the devices to the device using the PS/2 driver, add another grep for PS/2.

xinput -list | grep point | grep PS/2

Now we have the device we want to enable and disable we cut to get the device id, I used a cut with a delimiter of "=" and selected field 2, which would provide everything past the = sign (you could use sed /awk or a number of other options). finally it is safe to cut two characters as we are not going to have hundreds of input devices.

xinput -list | grep -i Point | grep "PS/2" | cut -d "=" -f 2 | cut -b 1-2

To get the enabled peramiter ID

touchPadId=11
xinput --list-props $touchPadId | grep -i enabled

From the above we can see the enabled property ID for this device on this system is 125, we trim this with sed;

xinput --list-props $touchPadId | grep -i enabled | sed 's/.*(//' | sed 's/).*//'

To set the device enabled or disabled

enabledId=125

Enabled:
xinput set-prop $touchPadId $enabledId 1

and

Disabled:
xinput set-prop $touchPadId $enabledId 0

ALL Done :)

Post a Comment