12

I see that I can configure mouse and touchpad all in one panel (Ubuntu 13.10, defaults, Gnome). Which is to some extend. Namely, I want to have the touchpad differently configured then the mouse and ...

When I have an (USB) mouse attached I use it normally left-handed. Therefore I switch the buttons to left-handed. But when I use the touchpad I feel little bit lost with that kind of configuration. Therefore I wanna have it right-handed.

Is there a way to achieve the following: when I plugin a mouse (respectively when it is detected) automatically switch to left-handed and when I unplug it, switch automatically to right-handed?

5 Answers 5

11

I understand your frustration, but this problem can be solved very simple with the help of xinput tool.

First plug in your USB mouse, then run the following command:

xinput list

to see the id of your mouse. The output of above command it can be similar to:

xinput | cat
⎡ Virtual core pointer                      id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎜   ↳ SynPS/2 Synaptics TouchPad                id=14   [slave  pointer  (2)]
⎜   ↳ USB Mouse                                 id=11   [slave  pointer  (2)]
⎣ Virtual core keyboard                     id=3    [master keyboard (2)]
    ...

In the above example the USB mouse has id=11. We will use this id in the following command which will swap buttons to be left handed only for the USB mouse (and not for tauchpad):

xinput set-button-map 11 3 2 1

In general:

xinput set-button-map id 3 2 1

To revert the change, use:

xinput set-button-map id 1 2 3

To make the change permanently, add the following command at Startup Applications (search in Dash for Startup Applications):

sh -c "xinput set-button-map id 3 2 1"

Update:

Since the id might change after reboot but the name of the USBmouse not, you could also grep for the name of the mouse and apply it. To skip the details reg. picking out the name the final solution looks like:

for id in `/usr/bin/xinput list | /bin/grep 'USB Mouse' | /bin/grep -o [0-9][0-9]`; do xinput set-button-map $id 3 2 1; done;

pack it into the above mentioned Startup Applications you'll get finally:

sh -c "for id in `/usr/bin/xinput list | /bin/grep 'USB Mouse' | /bin/grep -o [0-9][0-9]`; do xinput set-button-map $id 3 2 1; done;"
3
  • 2
    I don't know, if I should edit your post or not. Your approach gave me the right direction, but the problem was/is that a restart might change the id of the mouse. Therefore I changed it into sh -c "for id in '/usr/bin/xinput list | /bin/grep 'Razer' | /bin/grep -o [0-9][0-9]'; do xinput set-button-map $id 3 2 1; done;" and this works (perhaps one has to care about the proper hyphens). Thx for the hint.
    – LeO
    Nov 15, 2013 at 8:43
  • @LeO In my case a reboot, doesn't change the id of the mouse. I didn't test with more mice or to change the position of USB port. Nov 15, 2013 at 8:48
  • @LeO Anyway, your idea is very good, so, go ahead, please edit the answer. If not, I will do it latter. Nov 15, 2013 at 8:52
10

There's an even simpler way than in Radu Rădeanu's answer. xinput accepts the name of the input device as an alternative to its numerical ID. Since your USB mouse is probably treated generically, it's name is most likely USB Mouse. Therefore you likely don't even need to run xinput list.

Thus your xinput set-button-map, with this name in place of the numeric ID, becomes:

xinput set-button-map "USB Mouse" 3 2 1

This takes the place of xinput set-button-map id 3 2 1 and should achieve the same thing, with 3 2 1 making it a left-handed mouse, just like when the numeric ID way is used. (And replacing 3 2 1 with 1 2 3 will still work to switch it to being a right-handed mouse.)

This has the advantage that, provided it works initially, it will continue to work across reboots, without having to obtain the device's ID number using long, complex commands.

2
  • 1
    This answer do deserves more upvotes.
    – Mephisto
    Oct 9, 2015 at 23:43
  • In my case, I have a wireless mouse + keyboard, so xinput list shows 2 entries with the same name ("2.4G Wireless Keyboard&Mouse") except one says "[slave pointer (2)]" and the other says "[slave keyboard (3)]". So with your solution I have this error "There are multiple devices named '2.4G Wireless Keyboard&Mouse'.". By the moment, I'm using Radu's solution adding grep 'slave pointer'. I wonder if xinput allows to somehow specify we are referring to a pointer without doing all this. Aug 25, 2017 at 15:48
1

I found that the startup command in the other answer only works if the id is a two digit number. Here are my modifications which should work for both one and two digit numbers:

In Startup Applications

bash -c "for id in `/usr/bin/xinput list | /bin/grep 'USB Mouse' | /bin/grep -o id=[0-9][0-9]*`; do xinput set-button-map ${id:3} 3 2 1; done;"
0
device_id=$(/usr/bin/xinput list \
            | /bin/grep 'slave  pointer' \
            | /bin/grep 'Wireless Keyboard&Mouse' \
            | tr " " "\n" \
            | /bin/grep "id=" \
            | /bin/grep -o [0-9][0-9]* \
        )
/usr/bin/xinput set-button-map $device_id 3 2 1 

Here is a more verbose solution that solves two problems:

  • It works for device ids with any number of digits (same as @robbie solution, but written a bit different)
  • Considers that the device name may be duplicated. In my case, I have a wireless mouse + keyboard, so xinput list shows 2 entries with the same name ("2.4G Wireless Keyboard&Mouse") except one says "[slave pointer (2)]" and the other says "[slave keyboard (3)]". So with @reedstrm solution I had this error "There are multiple devices named '2.4G Wireless Keyboard&Mouse'.", while with @Radu Rădeanu solution, the for was iterating over 2 ids, one for the mouse and one for the keyboard, having a "device has no buttons" error message with the second.
0

This question is full of aged answers. As of 2023, the best method is with input-remapper which not only track devices for configurations separately, but also run as a service so everytime when you restart the computer the settings remains. Detail answer here Set left-handed mouse and right-handed touchpad

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .