Bind Mouse Buttons to Keys or Scripts under Linux with xbindkeys and xvkbd

Xilin Sun
Xilin Sun
Jan 6, 2018 · 2 min read

So, I have bought this Logitech MX Ergo Plus wireless trackball and want to use it under Linux. All buttons work out of the box, but I want to customize some of them. On Windows there is Logitech software to do the rebinding job, while on Linux I can achieve more with xbindkeys and xvkbd. This should work for most mice and trackballs under X (probably not under Wayland).

Install software

yaourt -Sy xbindkeys xvkbd

Get button codes

Run xev in terminal, press one button on the mouse, and watch the console output. The button number can be found there. For example, here is the output for left button:

ButtonPress event, serial 38, synthetic NO, window 0x4000001,
root 0x398, subw 0x0, time 22026698, (73,117), root:(74,182),
state 0x4, button 1, same_screen YES
ButtonRelease event, serial 38, synthetic NO, window 0x4000001,
root 0x398, subw 0x0, time 22026818, (73,117), root:(74,182),
state 0x104, button 1, same_screen YES

The left button is recognized as button 1.

Using the same method, I have got the codes for all the (usable) buttons on my MX Ergo:

1: left button
2: mid button (scroll wheel clicked down)
3: right button
4: scroll up
5: scroll down
6: scroll wheel tilt left
7: scroll wheel tilt right
8: back button
9: forward button

I marked all these buttons in the picture below. The switching device button and precision button are not avilable for the operating system.

Bind buttons

By default, buttons 9 and 8 work as back and forward in the browser, but I think they will be more useful if working as tab switchers (Ctrl-PgUp and Ctrl-PgDn). Also, I want to bind buttons 6 and 7 as closing tab and new tab. To do this, I add the following part to my ~/.xbindkeysrc file.

# Button 6 closes a tab
"xvkbd -text '\Cw'" # Sends Ctrl-w
b:6
# Button 7 opens a tab
"xvkbd -text '\Ct'" # Sends Ctrl-t
b:7
# Button 8 sends Control + PageDown
"xvkbd -text '\C\[Next]'" # Sends Ctrl-PgDn
b:8
# Button 9 sends Control + PageUp
"xvkbd -text '\C\[Prior]'" # Sends Ctrl-PgUp
b:9

The syntax should be pretty self-explanatory.

I can replace the "xvkbd..." line with any script to achieve other functions.

xvkbd documentation can be found at http://t-sato.in.coocan.jp/xvkbd/#option and xbindkeys's can be found at http://www.nongnu.org/xbindkeys/.

Xilin Sun

Written by

Xilin Sun

👨👩🐶🏊🚲🏸🏍️🌉 https://github.com/aenon

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade