Note: This isn’t directly related to KDE. However, since there isn’t such a tool for KDE at the moment, I figured some KDE users might find it interesting as well.
I recently bought a new mouse for my desktop computer, more specifically a Logitech Corded Mouse M500. Up until then I had been using an old mouse with two buttons and a scroll wheel. Now I suddenly have two extra mouse buttons, and a scroll wheel that you can tilt to the left and right. Let’s see how to take advantage of this.
Bind keys with xbindkeys
As the name suggests,
xbindkeys is a small program that lets you bind hotkeys to actions under X. Although it doesn’t include a GUI, it’s very easy to use. There is a graphical configuration tool called
xbindkeys-config (GTK frontend), but in this guide I’ll edit the config file directly.
- First of all you need to install
xbindkeys, preferably through your package manager.
- You can create a default configuration file with the command
xbindkeys --defaults > $HOME/.xbindkeysrc
~/.xbindkeysrcin your your favorite text editor. (If you can’t find it, make sure you have “Show hidden files” enabled).
[This guide aims to be quite comprehensive - if you don't want to read everything, you can look at the default configuration file and try to figure out the rest yourself. It's not difficult if you read the included comments]
- Let’s check the syntax. Everything after
#are comments, which means that the program will ignore them.
You can find the following text in the default file:
# The format of a command line is: # "command to start" # associated key
Below are also some examples – great. It’s probably a good idea to include a short comment (start the line with
#) before each command, for example
# short comment "command to start" associated key
"command to start"is simply a shell command (that you can run from a terminal). I’ll talk more about it under Actions.
associated keyis the hotkey. Let’s take a closer look at how to find what we should write here. But before that, don’t forget the last step.
xbindkeysby running the command in a terminal.
This section deals with keyboard shortcuts. If you’re only interested in mouse shortcuts, please see the next section Mouse shortcuts.
The first example in the generated
.xbindkeysrc shows how you can define a hotkey:
control+shift + q
A list of modifiers is included in the comments.
# List of modifier: # Release, Control, Shift, Mod1 (Alt), Mod2 (NumLock), # Mod3 (CapsLock), Mod4, Mod5 (Scroll). # # The release modifier is not a standard X modifier, but you can # use it if you want to catch release events instead of press events # # By defaults, xbindkeys does not pay attention with the modifiers # NumLock, CapsLock and ScrollLock.
Pretty straightforward, don’t you think? You can also use raw keycodes, as shown in the next example:
c:41 + m:0x4
Now this might look cryptic, but it’s really no problem – you can easily find the keycodes with the
--key option. Run
in a terminal and a window pops up. Press the desired hotkey and you should get an output similar to this:
Press combination of keys or/and click under the window. You can use one of the two lines after "NoCommand" in $HOME/.xbindkeysrc to bind a key. "(Scheme function)" m:0x15 + c:34 Control+Shift+Mod2 + q
In this case I pressed
Mod2 corresponds to Numlock which is ignored by default, as explained in the earlier text about modifiers. Apparently the keycodes for this hotkey are
m:0x15 + c:34.
Now that we know how to deal with keyboard hotkeys, mouse shortcuts should be a piece of cake. The first mouse button (left) is called
b:1, the second (right)
b:2 and so forth. Take a look at the third example and you’ll see how a mouse shortcut can be defined:
control + b:2
That’s right, you can combine mouse and keyboard shortcuts – great for those who don’t have a fancy mouse with extra buttons. I used to bind
Super+ mouse buttons to various actions with my old mouse (
Super is the Windows key on most keyboards).
To find out what a mouse button is called, you can use
xev. Similar to
xbindkeys -k that you used earlier, it pops up a window. However, this one isn’t closed automatically, and it also grabs mouse movement and clicks. Try to click/scroll in the window (without too much mouse movement), and you should see something like
ButtonPress event, serial 31, synthetic NO, window 0x3a00001, root 0x189, subw 0x0, time 55432271, (4,0), root:(1102,842), state 0x10, button 9, same_screen YES
in the terminal output. According to the last line, I just pressed mouse button 9, which translates to
command to start is a command that you can run from a terminal, for example an application (
dolphin) or a shell script (out of scope for this article).
Let’s take a concrete example. I want to bind wheel tilt -> switch to previous/next desktop. To do this I have to communicate with KWin, the default KDE window manager. KDE applications use D-Bus to communicate with each other, and you can “talk” to them with D-Bus as well. (This is for KDE SC4, in the KDE SC3 series DCOP is used instead).
I’m not very familiar with D-Bus myself, but with the help of the
qdbusviewer tool and some Googling I found that I can use the following command to switch desktop:
qdbus org.kde.kwin /KWin org.kde.KWin.nextDesktop
xev (explained in Mouse shortcuts) the tilt to the left and right corresponds to
b:7. Therefore, a part of my
.xbindkeysrc looks like this:
# Previous desktop "qdbus org.kde.kwin /KWin org.kde.KWin.previousDesktop" b:6 # Next desktop "qdbus org.kde.kwin /KWin org.kde.KWin.nextDesktop" b:7
Don’t forget the quotation marks (
"") around the commands!
Another tool you can use to find D-Bus calls is
dbus-monitor, as suggested by Balden in the comments. Run it in a terminal and trigger a command the normal way, for example by hitting a keyboard combination, and
dbus-monitor will show the call(s).
Emulate key presses
Now I want to map the two other mouse buttons to the KWin effects “Present Windows” and “Desktop Grid”. I couldn’t find any D-Bus calls for this, so let’s try to achieve it in another way. The effects are already mapped to some keyboard shortcuts – maybe we can find a command to “emulate” these key presses?
The tool for doing this is called
xte, and is included in the
xautomation package. Again, install it with your package manager.
The default shortcut for “Show Desktop Grid” is
Ctrl+F8. The following command should then activate the effect:
xte 'keydown Control_L' 'key F8' 'keyup Control_L'
Whether we choose
Control_R doesn’t really matter. After confirming that it works, I added the following lines to
# Present Windows "xte 'keydown Control_L' 'key F10' 'keyup Control_L'" b:8 # Desktop Grid "xte 'keydown Control_L' 'key F8' 'keyup Control_L'" b:9
If nothing happens for you, make sure you emulate the right key presses. Have you changed any shortcuts?
This trick can also be used to map the back/forward buttons on the mouse. Here we can’t use D-Bus, since we want to “talk” to the active window and not a specific application. Fortunately for us, most applications use the key shortcuts
Alt+Right for Back and Forward, respectively. In other words, we can do something like this:
# Back "xte 'keydown Alt_L' 'key Left' 'keyup Alt_L'" b:6 # Forward "xte 'keydown Alt_L' 'key Right' 'keyup Alt_L'" b:7
This’ll work in Dolphin, Konqueror, Firefox, and basically any application that use the same shortcut for Back/Forward.
xte can also be used to simulate mouse clicks, as pointed out by Rahul (see comment for an example).
Want to know more about
xte --help displays some helpful text that you might find useful.
Apply settings and make xbindkeys autostart
Now we’re almost done! Remember that you have to restart
xbindkeys to apply the changes:
killall xbindkeys && xbindkeys
As a final touch, let’s make
xbindkeys autostart with KDE. It’s very easy to do with the new Autostart module.
- Open System Settings.
- Go to Advanced tab -> Autostart.
- Click on “Add Program…”.
xbindkeysand press OK. A new dialog pops up. Press OK again.
In this tutorial we’ve learned how to use the
xbindkeys program to bind keyboard and mouse shortcuts to different commands. We also took a quick look at how to communicate with an application using
qdbus and emulate key presses with
xte. Finally, we made
I think it would be great if the “Input Actions” module in System Settings also supported mouse shortcuts. A more simple module for assigning mouse buttons to actions would be nice as well.
Hope that you found this guide helpful, don’t hesitate to leave a comment below if there’s anything on your mind.