Mapping Your Mouse Buttons in Linux

At my new gig at Red Hat I've been trying to use the mouse less and less but still find myself using it occasionally so why not tweak it and use it efficiently?

Digging around I found xbindkeys.

Xbindkeys is a program that enables us to bind commands to certain keys or key combinations on the keyboard. Xbindkeys works with multimedia keys and is window manager / DE independent, so if you switch much, xbindkeys is very handy.

Xbindkeys is available in most repos. In addition I've found you need a few more items as well:

sudo dnf install xbindkeys xautomation xev

xbindkeys - the main program

xautomation - Control X from the command line for scripts, and do "visual scraping" to find things on the screen.

xev - Xev creates a window and then asks the X server to send it events whenever anything happens to the window

I have a Logitech M705 mouse. In addition to the 3 main mouse buttons it has two buttons on the side and the mouse wheel will tilt left and right giving me 4 additional buttons I can use.

First thing is to figure out what your mouse is sending out so you can use those values in a script.

We can use xev for this.

If you start xev a terminal will pop up which contains a small white box. You can carefully hover your mouse over that box and click a button and it will output information. Actually it outputs a LOT of information! Every time you move the mouse it will output something. Since all we are concerned about is a button click we can filter out a lot of the noise:

xev | grep button

Now I can click both side buttons and tilt the mouse wheel button and I will see something like:

Here I am simply mapping my CTRL+ALT+LEFT series of key presses via script and mapping those keys to my mouse button which we identified above (b:8).

When I click my mouse button - my workspace will switch!

Also note xbindkeys can also bind your keyboard! So similar to some of my AutoHotkey scripts - you could also bind an additional key to provide even more actions on your mouse buttons. For example button 8 may switch workspaces. SHIFT+button 8 may launch my browser.