2 mice are not better than 1
January 19th, 2007 | Published in Tutorial | 16 Comments
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





January 19th, 2007 at 12:14:43 (#)
The touchpad on my Compaq laptop has an on/off button, so I just turn it off when I don’t need it
January 19th, 2007 at 13:01:11 (#)
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
January 19th, 2007 at 13:13:50 (#)
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.
January 19th, 2007 at 13:14:44 (#)
And… I stand corrected. Don’t even bother to respond
January 19th, 2007 at 13:23:26 (#)
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).
January 19th, 2007 at 13:55:35 (#)
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.
January 19th, 2007 at 13:57:44 (#)
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!
January 19th, 2007 at 14:49:27 (#)
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
January 19th, 2007 at 14:58:22 (#)
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.
January 19th, 2007 at 16:14:41 (#)
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
January 19th, 2007 at 16:15:35 (#)
sorry, i closed code tag to early
admin, feel free to edit it
January 19th, 2007 at 16:59:29 (#)
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.
January 20th, 2007 at 02:54:19 (#)
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!
May 16th, 2007 at 03:02:39 (#)
[...] nixternal publicaron hace tiempo un pequeño script para correr al inicio que comprueba si hay conectado un [...]
January 27th, 2008 at 04:42:44 (#)
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.
January 27th, 2008 at 04:43:59 (#)
Also, this website has a bug where it ends code blocks prematurely.