Blog Post

2 mice are not better than 1

Since I have gotten my new laptop I have quickly become annoyed that when I plug in my external USB wireless mouse my synaptics touchpad is still active. The touchpad is fine when I am not using an external mouse, but I can’t stand tapping it while I am typing. So yesterday while making some jokes towards Cody Somerville and Martin Meredith on writing a program to fix it, they said a quick script could take care of it in the mean time. So what I did is wrote an elaborate, super hacker, elite bash script that starts with X. So you want to see it? Well, I can show you that and not have to kill you afterwards! Here it is:

mousecount=`grep mouse /proc/bus/input/devices |grep Handler |wc -l`
if [ “$mousecount” -eq “1” ]
then
synclient TouchpadOff=0
else
if [ “$mousecount” -gt “1” ]
then
synclient TouchpadOff=1
fi
fi

What this does is executes the grep command and assigns the output to $mousecount. The first “if” statement tests to see if only 1 mouse is active, in my case it would be the touchpad and no external mouse, so it will insure it doesn’t get deactivated. If that doesn’t test out, the next “if” statement will see if it is equal to 2, meaning I have more than 1 mouse attached, the 2nd being the external USB wireless mouse. If it sees 2 mice, then it will execute the “synclient TouchpadOff=1” command disabling the synaptics touchpad.

Pretty easy I think. Now if someone can come up with an even better way of doing this, please keep me informed. My little script is rather hackish, but it does what I want it to. I would love to see an application do this automatically when I plug or unplug my external mouse. Any takers πŸ™‚

This entry was posted in Linux and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • Nanda

    The touchpad on my Compaq laptop has an on/off button, so I just turn it off when I don’t need it πŸ™‚

  • I have the Compaq Presario C304NR laptop, and I do not see a button to shut it off πŸ™‚ That would be nice as well, but I know with Windows, and even OS X it will autodetect the external mouse and disable the touchpads. We just need Ubuntu/Linux to do the same πŸ™‚

  • hggdh

    you probably meant ‘cat /proc/bus/input/devices | grep mouse | grep Handler | wc -l’

    At least this is how it looks under 2.6.20.

  • hggdh

    And… I stand corrected. Don’t even bother to respond πŸ™‚

  • hggdh

    also, the KDE utility ksynaptics will do it on the fly, by pressing Ctrl/Alt/P to disable or enable the touchpad.

    I believe there is something equivalent under Gnome, but — if I remember correctly –, it does not allow hot-keys (you have to run it manually out of the Gnome settings, or something like it).

  • sil

    You ought to be able to put this somewhere under /etc/hotplug to have it happen when the mouse is plugged in or unplugged. Where exactly that would be is a problem left to the reader. πŸ™‚

  • hggdh, ya, I totally stopped doing cat /blah/blah |grep blah as you can just grep from the get go, shrinks the command a little bit, but still works the same no matter which way you go.

    Well, the only thing with KSynaptics is you have to “manually” change the settings. And I also noted that with KSynaptics shutting the touchpad off during typing didn’t work either. Bug time possibly. KSynaptics might be a damn good start though to what I would really like to accomplish, and that is automatic on/off when i plug/unplug my external mouse. Thanks for waking me up to KSynaptics!

  • Lure

    nixternal, Tonio_ had plans to propose ksynaptics for main in Feisty and include it in default install. I am not sure if this is still in his plans, but you should ping him on IRC about this.
    See: https://wiki.ubuntu.com/KubuntuFeistyLaptop

  • At least on feisty in the Gnome Control Center I have an option called “Settings for removable media” (or something likewise, am translating from danish).
    It has an option to run a program when an USB mouse is connected.

  • here you got it:


    echo 'KERNEL=="mouse*", RUN+="/usr/bin/udevmice.sh"' >> /etc/udev/rules.d/85-hal.rules

    then create this script in /usr/bin/udevmice.sh:

    #!/bin/bash

    DATE=`date`
    FLOG="/tmp/udev_test.log"
    echo "$DATE - [$*] - [$ACTION]" >> "$FLOG"

    case $ACTION in
    add)
    echo "DEBUG: conectat" >> "$FLOG"
    synclient TouchpadOff=1
    ;;
    remove)
    echo "DEBUG: desconectat" >> "$FLOG"
    synclient TouchpadOff=0
    ;;
    esac

    that’s all

    πŸ™‚

  • sorry, i closed code tag to early πŸ™

    admin, feel free to edit it

  • ROCK ON WITH YOUR BAD SELF!

    Dude you are the champ! Thanks a lot for this one πŸ™‚

    I have been studying udev and was getting close but this totally helped, worked, and rocks! Thanks again.

  • Mez

    Darn you muzzol!

    I was waiting for nixternal to be active online before proposing a similar thing πŸ˜‰

    I didn’t know this was here until now!

    πŸ™

  • Pingback: Desactivar el touchpad al conectar un ratΓ³n en Linux()

  • Esekla

    Here is my version of udevmice.sh, combining the two comments above so that it both honors (un)plug events and handles the case when the mouse is already plugged in when the x-session starts up:

    #!/bin/bash
    #DATE=`date`
    #FLOG="/tmp/udevmice.log"
    #echo "$DATE - [$*] - [$ACTION]" >> "$FLOG"
    #env >> "$FLOG"

    if ps -C x-session-manager>/dev/null ; then
    case $ACTION in
    add)
    synclient TouchpadOff=1
    ;;
    remove)
    synclient TouchpadOff=0
    ;;
    *) # assume X startup
    /usr/bin/syndaemon -d -i 1.0 -t
    if lsusb|grep -q Mouse ; then
    synclient TouchpadOff=1
    else
    synclient TouchpadOff=0
    fi
    ;;
    esac
    fi

    I put it in my session startup, and also use the udev rule mentioned above to call it for plug events. Note that my mouse is usb (and my touchpad is not) so I detect the presence of a plugged in mouse with lsusb. I’m a lousy scripter, so refinements are welcome.

  • Esekla

    Also, this website has a bug where it ends code blocks prematurely.

  • Subscribe to nixternal.com

     Subscribe in a reader

    Or, subscribe via email:
    Enter your email address:

  • Archives


semidetached
semidetached
semidetached
semidetached
%d bloggers like this: