2 min read

How to bind mouse keys to keyboard keys in Linux (Mint)

Coming from macOS where I used swipes on my Magic Mouse, one of the things I missed when I started using Linux extensively, was the fact that I had to use either my keyboard or click on Workspaces in my panel to switch to another workspace.

I'm using a simple mouse that literally has only 5 buttons - 2 regular (left and right), a middle/scroll button and two buttons on a side. I think that by default those buttons can be used in a browser to switch between previous pages, but there was no use for them in the system itself. When I used the same mouse on macOS, I had to install a separate app that allowed me to map those keys to certain actions, so I wanted to do the same when I use Linux.

To achieve this, there's a program on Linux that can do exactly what I wanted, xbindkeys.

So let's start by installing the program (Debian-based systems):

sudo apt install xbindkeys x11-utils xdotool

Now we have to find out how mouse keys are represented in the system. To do that, we will use xev:

xev | grep button

This will open a small window that we will use to click with our side buttons:

With this window focused, press your side buttons (or whatever buttons you want to configure), and the output should look something like this:

> xev | grep button
    state 0x0, button 8, same_screen YES
    state 0x100, button 8, same_screen YES
    state 0x0, button 9, same_screen YES
    state 0x100, button 9, same_screen YES

Notice the ID of the buttons - in my case, those are buttons 8 and 9.

Next, we will want to use xev to find out what keycodes are for our actions. Since I want to use my mouse buttons to switch between workspaces, I have to use keyboard shortcuts to do that - I believe that default shortcuts to do that are Ctrl+Alt+ArrowKeyLeft/Right. So, if I want to switch to the left, I will use Ctrl+Alt+Left, if I want to switch to the right workspace, I will use Ctrl+Alt+Right. With xev running, I can get those keycodes:

> xev | sed -ne '/^KeyPress/,/^$/p'
KeyPress event, serial 36, synthetic NO, window 0x6800001,
    root 0x905, subw 0x0, time 10916617, (1785,1510), root:(1822,1704),
    state 0x0, keycode 37 (keysym 0xffe3, Control_L), same_screen YES,
    XLookupString gives 0 bytes: 
    XmbLookupString gives 0 bytes: 
    XFilterEvent returns: False

KeyPress event, serial 36, synthetic NO, window 0x6800001,
    root 0x905, subw 0x0, time 10916617, (1785,1510), root:(1822,1704),
    state 0x4, keycode 64 (keysym 0xffe9, Alt_L), same_screen YES,
    XLookupString gives 0 bytes: 
    XmbLookupString gives 0 bytes: 
    XFilterEvent returns: False

Notice the keycodes for each of the action: keycode 37 and keycode 64.

With this information available, we can now setup our config file:

xbindkeys -d > ~/.xbindkeysrc

Once the file is created, we can edit it in our favourite editor and add the configuration like this at the end of the file:

"xdotool key 'Control_L+Alt_L+Left'"
       b:9
"xdotool key 'Control_L+Alt_L+Right'"
       b:8

This configuration enables buttons 8 & 9 on my mouse to perform actions that would usually be achieved by pressing keyboard keys.

You may have to log out of the system to enable these changes, but since xbindkeys starts automatically when you log into the system, that's about it. No other changes are necessary after that.

Hope this article helps you with customising your Linux installation!