How to Map a Mouse Button in Linux?

4

This tutorial shows how to map a mouse button in Linux both from the command line and the GUI.

Map a Mouse Button in Linux from the Command Line:

To map your mouse from the command line you need xinput, a command line utility to configure and test X input devices. Xinput allows you to show available input devices, query information about a device and edit input device settings.

To install xinput in Debian based Linux distributions, run the following command:

$ sudo apt install xinput

When you run xinput, the output will show input devices such as keyboard, mouse, web cam, etc. As you can see my detected mouse is USB OPTICAL MOUSE with ID 10. The ID is the information you will use in next steps.

Now, you need to get your mouse buttons map. For this purpose, you need to use xinput with the option get-button-map for the ID 10, as shown in the example below:

$ xinput get-button-map 10

As you can see, there are 7 buttons, including directions for the mouse. You can learn the buttons functions using the list parameter as shown in the following example.

Now, you need to identify each button. You can do it by using the test option for the device 10 as shown in the following example. Once executed, press each key and the output will return its number.

Alternatively, you can also use the command xev to identify keys and buttons as shown in the example below. Position your pointer inside the white box and press the buttons or keys you want to identify.

My left click is the number 1, and my right click is the number 3. To invert the buttons, you need to use the set-button-map option, followed by the mouse ID and the button map. In this case, I replaced 1 with 3, and 3 with 1, left with right and right with left.

$ xinput set-button-map 10 3 2 1 4 5 6 7

Now, test your buttons.

Xinput also can be used to map your keyboard or other different input devices (like joysticks). For more information on XInput you can visit https://linux.die.net/man/1/xinput

Mapping your Mouse from the Command Line (Xmodmap):

You also can map your mouse using Xmodmap. In this case we will map a mouse button with the keyboard. To begin, install xkbset by running the command below:

$ sudo apt install xkbset


On Arch Linux, you can run:

$ sudo pacman -S xorg-xmodmap xorg-xev xorg-setxkbmap
yay -S xkbset

Now, generate a Xmodmap configuration file by running the command below. Then, open the created file using a text editor in the example below I use nano.

$ xmodmap -pke > ~/.Xmodmap

The keycode column represents a key. You also can identify the keys using the command xev used previously.

For example, to give a key left-click function, it should be defined as Pointer_Button1. To define a key as a left click, it should be Pointer_Button3.

You can also use xmodmap as a command, for example, to turn the menu key into a left click run:

$ xmodmap -e “keycode 135 = Pointer_Button1”

Then log out and login back to apply the changes. Keep in mind the changes will be only persistent if saved in the configuration file.

For additional information on Xmodmap you can read the man page https://www.x.org/archive/X11R6.8.1/doc/xmodmap.1.html.

Map a Mouse Button in Linux from the GUI:

This section shows how to map mouse buttons from the graphical user interface using Key Mapper.  Before starting install the package called python-pydbus using apt as shown below:

$ sudo apt install python-pydbus

Now download Key Mapper. You can download Key Mapper from https://github.com/sezanzeb/key-mapper/releases/

You also can get it using wget as in the example below (remember to update the version).

Note: For other Linux distributions, download the .tar.gz package.

$ wget https://github.com/sezanzeb/key-mapper/releases/download/1.0.0/key-mapper-1.0.0.deb

Once downloaded on Debian based Linux distributions, run the command below:

$ sudo dpkg -i key-mapper-1.0.0.deb

Execute Key-Mapper by running the command below:

Key Mapper is fully intuitive. On the key side, press the key you want to map. Remap the button on the Mapping column, then press twice on the Apply button. You can always press CTRL+DEL to restore default settings.

Conclusion:

As you can see, mapping mouse buttons in Linux is an easy task any Linux user level can learn and apply. This function is especially useful in case of a broken input device or when an input device isn’t supported by our Linux. Some users may use the tips explained above also to configure joysticks for gaming or for accessibility.

I hope this tutorial was useful. Keep following Linux Hint for more Linux tips and tutorials.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More