Map Mouse Buttons in Ubuntu

Map_Mouse_Keys.pdf

Special HACK when using mouse with ONLY 3 buttons and you want pushing the scroll wheel to close tab.

# NOTE: SIMPLE MOUSE - SCROLL WHEEL CLICK TO CLOSE TAB
"xdotool key 'Control_L+w'"
        b:2

Sample Bindings:
xbindkeysrc

meld ~/.xbindkeysrc ~/Desktop/xbindkeysrc.txt

Refer: https://www.linuxuprising.com/2019/11/how-to-bind-mouse-buttons-to-keyboard.html

Install the tools:

sudo apt install xbindkeys x11-utils xdotool

Grab the mouse button codes:

xev | grep button

Next, focus the small window that pops up and watch the terminal output. Now press the mouse button for which you want to grab the code. After pressing the button you should see its code in the terminal where you ran xev, e.g.:

Results

Left Scroll Wheel Click
state 0x10, button 6, same_screen YES

Right Scroll Wheel Click
state 0x10, button 7, same_screen YES

Grab the keystrokes that we'll later send using a mouse button (skip if you want to bind a command / script / program to a mouse button)

Run the following command:

xev | sed -ne '/^KeyPress/,/^$/p'

For [Ctrl]+[w], results

KeyPress event, serial 37, synthetic NO, window 0x3200001,
    root 0x5a9, subw 0x0, time 1609319, (114,64), root:(200,166),
    state 0x10, keycode 37 (keysym 0xffe3, Control_L), same_screen YES,
    XLookupString gives 0 bytes: 
    XmbLookupString gives 0 bytes: 
    XFilterEvent returns: False

KeyPress event, serial 37, synthetic NO, window 0x3200001,
    root 0x5a9, subw 0x0, time 1609946, (114,64), root:(200,166),
    state 0x14, keycode 25 (keysym 0x77, w), same_screen YES,
    XLookupString gives 1 bytes: (17) ""
    XmbLookupString gives 1 bytes: (17) ""
    XFilterEvent returns: False

Note down the keycode, keysim or name of the key. You can use any of the 3, e.g. for the first key above you can use either 37, 0xffe3, or Control_L. In this article I'll use the key name (e.g. Control_L for left control key) since they are easier to read.

Create the xbindkeys configuration (in ~/.xbindkeysrc).

You may either create an empty ~/.xbindkeysrc file, or generate a sample configuration file using this command:

xbindkeys -d > ~/.xbindkeysrc

Next, open ~/.xbindkeysrc with your favorite text editor. Note that .xbindkeysrc is a hidden file in your home directory so you'll need to press Ctrl + h (to show hidden files and folders) to see it in your file manager.

To bind a mouse button to a key combination, paste the following at the end of the ~/.xbindkeysrc file (it may already contain some key binds, it may contain the sample configuration or it can be empty - it doesn't matter):

"xdotool key 'KEY-COMBINATION'"
       b:MOUSE-BUTTON-CODE

Where:

  • KEY-COMBINATION are the keystrokes you got under step 3 (separate the keys with a + sign)
  • MOUSE-BUTTON-CODE is the mouse button code that you got under step 2

Example:

# NOTE: MAP LEFT SCROLL WHEEL CLICK TO CLOSE TAB
"xdotool key 'Control_L+w'"
        b:6

# NOTE: MAP LEFT SCROLL WHEEL CLICK TO CLOSE WINDOW
"xdotool key 'Control_L+W'"
        b:7

This remaps the mouse button 8 (which for my mouse is the button that can be used as a back button in a web browser for example) to Control_L+Alt_L+Down which in GNOME Shell is used to switch to workspace down.

To bind a command, script or program to a mouse button, use this in the ~/.xbindkeysrc file:

"COMMAND"
       b:MOUSE-BUTTON-CODE

Here:

  • COMMAND is a command, script or program
  • MOUSE-BUTTON-CODE is the mouse button code that you got under step 2

Example for running a program using a mouse button:

"firefox"
       b:8

This starts Firefox when pressing mouse button 9 (which for my mouse is the button that can be used as a back button in a web browser for example).

Start xbindkeys.

Now you can start xbindkeys using a terminal and typing:

xbindkeys

In case xbindkeys was running, you can get it to use the new configuration by issuing; however, I found I needed to just reboot.

xbindkeys --poll-rc