Ubuntu 20.04 – Primary Button Resets on External Mouse

20.04mouse

I'm left-handed, so I have my primary mouse button set to the right button. This works fine on my touchpad buttons, and it used to work fine on my external Bluetooth mouse as well. But now suddenly, each time I connect the external mouse, the primary button on the mouse is the left one, while the touchpad primary button is still the right one. I can solve this by going to Settings > Mouse & Touchpad > Primary Button and set it to Left and then to Right. Of course, I'd rather not have to do this each time the mouse reconnects. Any suggestions?

Best Answer

I use a KVM switcher box and so I have the same issue every time I switch since upgrading.

At the moment I have this quick and dirty script. Run it in a terminal as the logged in user;

xinput list | grep Mouse | sed -nre 's/^.*\sid=([0-9]+)\s.*$/\1/p' | xargs -r -I '{}' echo xinput set-button-map {} 3 2 1

Hope to get something better but should be useful for now.

For completeness, it's worth noting that you can also use the mouse settings UI tool, and change the primary button to right handed and then back to left handed.

UPDATE/EDIT BELOW

I now have a more convenient solution that is automated for each kvm switch, but it's still a hacky/dirty solution, and I run a script after login...

Customise the following for your vendor id and product id, based on output of lsusb. Where the hex number with a colon in the middle is the {idvendor}:{idproduct} for your mouse. Also, as flipjacob adds, customise "Mouse" string in the grep statement to your match what you get from xinput.

Create a file (as root) /etc/udev/rules.d/80-force-left-handed-mouse-on-plugin-event.rules with the following contents

ATTRS{idVendor}=="045e", ATTRS{idProduct}=="0047", ACTION=="bind", RUN+="/root/notify-mouse-plugged.sh"

Create the script that it will run (as root) /root/notify-mouse-plugged.sh.

#!/bin/bash

umask 0000
echo $ACTION >> /tmp/mouse-flag
date >> /tmp/mouse-flag

And finally create the script to pick up the 'notification' in the context of the logged in user. I run this after logging in in a terminal and leave it running there.

Listener script $HOME/listen-for-mouse-plugged.sh.

#!/bin/bash

echo "" > /tmp/mouse-flag

tail -qfn 0 /tmp/mouse-flag 2>/dev/null | while read s
do
  #inotifywait -e create /tmp/mouse-flag
  echo reset mouse $s
  xinput list | grep Mouse | sed -nre 's/^.*\sid=([0-9]+)\s.*$/\1/p' | xargs -I '{}' xinput set-button-map {} 3 2 1

  #sleep 5
done

It fires the listener loop 3 times for me, but that is of little consequence for something I hope to throw away soon.