I struggled with this over the weekend, and need to remap my mouse buttons.
Mouse – How to Automatically Remap Buttons at Startup
mousexbindkeysxinputxte
mousexbindkeysxinputxte
I struggled with this over the weekend, and need to remap my mouse buttons.
Best Answer
I have a Logitech mouse with 9 buttons, and pressing the "middle button" (#2) involves clicking the scroll wheel. I dislike this because I'm clumsy and typically end up scrolling the window I'm in when I try to click the wheel. So I wanted to automatically remap the top side button (#9 in this case) to the middle button (#2). I also wanted to map the bottom side button (#8) so that it executes a double-click of the left button (#1).
Though my aims were specific, the solutions below can be generalized to any situation in which you want to automatically remap mouse buttons at startup.
Mapping Mouse Buttons to Other Mouse Buttons
You will need
xinput
installed for this task. This can be done entirely in your.xsessionrc
file. First, usexinput
to discover the name that is assigned to your mouse, which is then correlated to an input device ID. Below is some sample output from my laptop:My mouse calls itself
Logitech USB Laser Mouse
and is shown asid=11
. Your mouse will have a different name; figuring that out is left as an exercise for the reader.While you still know the ID of the device in this session, find out how many buttons the input handler thinks your mouse has, by using
xinput list
deviceID
. This may be different from the number of buttons that is apparent on the device.With my mouse, there are only 9 obvious physical buttons, but
xinput
reports 16.Given the nature of USB, this ID can change every time you restart, so it's not enough to script something that's statically keyed to an ID you discover once. You'll have to dynamically parse this at startup and execute your re-map based on the current ID.
Now that you know its name, you can use
xinput test
to figure out which key to remap. Press the mouse buttons you want to map from and to, in order to get their indices. (For reference, 1, 2, and 3 "always" (i.e., usually) refer to the left, middle, and right buttons of a 3-button mouse. A common re-map reverses these to make the mouse left-handed.)In this case I found that I want to map button #9 (side, top) to button #2 (middle).
Now that you know what your mouse is called, and which buttons you want to change, you can write an
~/.xsessionrc
script that invokesxinput
to execute the button re-mapping at startup. Below is my complete script.The first line here sets a temporary session variable equal to the ID of the mouse as reported by
xinput
. This is done bygrep
ing for the known name of the mouse in the report fromxinput
, then usingsed
to extract the ID number from thatid=xxx
token in the report. This value is then used in anxinput set-button-map
directive, which executes the re-mapping. In the example above, the only change is that button #9 is being re-mapped to mimic button #2. All others remain at their default setting.Update: As @Lokasenna points out below, if your device reports itself as both a mouse and a keyboard, you may need to limit the result count of the
grep
using-m 1
.Mapping Mouse Buttons to Arbitrary Functions
See also this answer.
You will need
xinput
,xbindkeys
, andxautomation
(includingxte
) installed for this task.Use
xinput list
andxinput test
to discover your mouse's device ID and the number of the button you want to assign. In my case, I wanted to map the bottom side button (#8) to a double-click of the left button (#1).Create or edit
~/.xbindkeysrc
. The format of this file is a series of paired lines. The first line is a command to be executed for an event; the second line is the event description. We will use thexte
component ofxautomation
to send events directly to the input handler.To create a double-click event when a button is released, I added the following:
This configuration maps a sequence of two mouse clicks on button #1 to the release of button #8. (In theory I guess you could map any command to a mouse button, but this is the most common case. See this answer for other practical examples.)
Update for 16.04 Ubuntu
For users with multiple mice attached to your system, you need to also pass in the ID of the device. This may not apply to all users and was discovered on Ubuntu 16.04 with Unity.
Then modify the .xbindkeysrc file by referencing the id= value from the command output (id=9 in this example):