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:

state 0x0, button 8, same_screen YES
state 0x0, button 9, same_screen YES
state 0x0, button 6, same_screen YES
state 0x0, button 7, same_screen YES

Now that we have identified the button numbers we can script what happens when you click them.

I make use of workspaces in Linux and sometimes it's a pain to flip to another workspace when I'm using the mouse. Normally I switch workspaces by pressing CTLR+ALT+LEFT (arrow key).

Xbindkeys is in many ways similar to AutoHotkey in that it can mimic key presses, so we simply need to map a mouse button to the same key presses:

Create a .xbindkeysrc file in your home directory.

###########################
# xbindkeys configuration #
###########################

# Workspace Left
 "xte 'keydown Control_L' 'keydown Alt_L' 'key Left' 'keyup Alt_L' 'keyup Control_L'"
  b:8

##################################
# End of xbindkeys configuration #
##################################

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.

Published on Wednesday, June 03 2015     Tags: linux tools

COMMENT / SHARE