Saturday 3 December 2011

HOWTO: Disable touchpad when mouse is plugged in

My Dell M4400 laptop runs Ubuntu Linux 10.04.  When writing reports, I kept making mistakes due to hitting the trackpad during typing.  To solve this problem, I'm using this script to disable the touchpad when my favourite mouse is plugged in.  When the mouse is unplugged, the script restarts the touchpad.  Very nice.

To use the script, first edit the script to check for the USB ID of your favourite mouse.  Then run the script manually in the Terminal to check it works.  If all is well, you can start it automatically at login by copying it into /usr/local/bin and adding this command under your System / Preferences / Startup Applications menu:
    /usr/local/bin/mouseSwitcher.sh >/dev/null 2>/dev/null &

For the sake of completeness, here is the script from the Ubuntu forum post linked to above.  I've added a comment or two and set my own mouse ID up, and fixed a bug relating to breeding syndaemon processes that eventually stopped X running.  Otherwise it's as the author wrote it.

BTW, rather annoyingly, syndaemon  doesn't actually work on my Dell M4400 with Ubuntu 10.04.  It's supposed to disable the trackpad for a second or two whenever you're typing.  But I thought I'd leave it in anyway as it will probably work on other hardware.

#!/bin/bash
#
# Toggle touchpad on and off
# Tested on Ubuntu 10.04
# Author: Heath Thompson
# Email:  Heath.Thompson (0at0) gmail.com
#
# NOTE you need to edit line 38 to match your mouse ID!!
#
# For startup wait for desktop to load first.
while true
do
    if ps -A | grep gnome-panel > /dev/null;
    then
        echo 'X loaded'
        break;
    else
        echo 'X not loaded, waiting...'
        sleep 5
    fi
done
#
# Check to see if appletouch is running
# if lsmod | grep appletouch > /dev/null;
# then
#     echo " * Appletouch enabled";
# else
#     echo " * Appletouch either not working or not installed"
#     killall mouseSwitcher.sh
# fi

killall syndaemon

while true
do
        # 'xinput list' will list all input devices x detects
        # I could reference my usb mouse by ID but I'm afraid that if I plug
        # another device in before my mouse, it might not have the same ID each
        # time.  So using the device name makes it relatively fail-safe.
        if xinput list 'HID 04b3:310b';  #EDIT THIS LINE TO MATCH YOUR MOUSE ID
        then
                # Found my usb wireless mouse
                # Disable everything on the Touchpad and turn it off
                synclient TouchpadOff=1 MaxTapTime=0 ClickFinger1=0 ClickFinger2=0 ClickFinger3=0;
                # Ends all syndaemon capturing which may have been used to monitor the touchpad/keyboard activity
                killall syndaemon
        else
                # My usb wireless mouse isn't present we need the touchpad
                # Reenable Touchpad and configure pad-clicks
                # RTCornerButton is the Right Top Corner on the touchpad
                #       The value 3 maps it as the right click button
                # RBCornerButton is the Right Bottom Corner on the touchpad
                #       The value 2 maps it as the middle click button
                synclient TouchpadOff=0 MaxTapTime=150 ClickFinger1=1 ClickFinger2=2 ClickFinger3=3 RTCornerButton=3 RBCornerButton=2;
                # Forces break of touchpad functions while typing if the touchpad is enabled.
                # Adds a 3 second interval following keyboard use which helps to prevent the
                # mouse from jumping while typing & resting hands on restpad or the touchpad
                # (Fix: only start syndaemon if we haven't already done so since last change of state.)
                if ! pgrep syndaemon >/dev/null ; then syndaemon -i 3 -d; fi
        fi

        # wait 2 seconds and poll the mouse state again
        sleep 2
done

sleep 15

An alternative approach is to make use of the udev framework to detect the USB mouse being connected.  This is documented here and here.

1 comment:

  1. Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.

    ReplyDelete

Spammers: please stop wasting my time. All comments are moderated before publication.