Advanced mouse configuration on Linux

"Confucius say: man and mouse the same, both end up in pussy."
On this page:

Saitek Cyborg RAT7 Mouse

Have a mouse with plenty of buttons but some of them don't work under Linux ? Fear not, here's a not so simple tutorial on how to configure those extra buttons. But before we get to the configuration of the buttons proper, my RAT7 mouse had two problems: it's too fast even at its lowest dpi setting, and it doesn't even seem to click normally !

1 - Mouse identification

First, let's identify the mouse name and its id. We'll use this later:

$ xinput list
 Saitek Cyborg R.A.T.7 Mouse               id=12   [slave  pointer  (2)]

Note: Some mice, such as those from Logitech, show up double: the first id is actually the mouse driver while the 2nd is a keyboard driver. It makes their configuration simpler, with all the buttons working out of the box on any OS (or that's the idea).


[WindowsVsLinux.jpg]
Breaking Windows.

2 - Slowing down the mouse

The following command can slow down any mouse that goes too fast on Linux. Play around with the '5' if the speed is not to your liking:

$ xinput --set-prop "Saitek Cyborg R.A.T.7 Mouse" "Device Accel Constant Deceleration" 5
And in order to have it activated automatically, I set up the following script in ~/bin/SetMouse.sh:
#!/bin/sh
xinput --set-prop "Saitek Cyborg R.A.T.7 Mouse" "Device Accel Constant Deceleration" 5
#xinput --set-prop "Saitek Cyborg R.A.T.7 Mouse" "Device Accel Velocity Scaling" 1

Don't forget to make it executable: $ chmod +x ~/bin/SetMouse.sh. The velocity scalling is optional.

And in KDE's [System settings][Startup and shutdown][Autostart] I add an entry in [Script file] pointing to that script. Done.

3 - Button identification

Run the following, then in the window that appears click with your various buttons and rotate you various wheels. Write down their numbers.

$ xev | grep button
    state 0x10, button 1, same_screen YES
    state 0x110, button 1, same_screen YES
    state 0x10, button 3, same_screen YES
    state 0x410, button 3, same_screen YES
    state 0x10, button 5, same_screen YES
    state 0x1010, button 5, same_screen YES
    ...

4 - Button removal

Apparently the RAT7 mouse has a 'mode' button with 3 configurations and one of them is always pressed, so it interferes with the other buttons. We'll simply remove it. As root, edit /etc/X11/xorg.conf and add (or change) the following section in order to remove the 'buttons' 13, 14 and 15. You can also use this to switch button (for instance "3 2 1 4..." will invert left and right click):

Section "InputClass"
    Identifier     "Mouse Remap"
    MatchProduct   "Saitek Cyborg R.A.T.7 Mouse"
    MatchDevicePath "/dev/input/event*"
	# Specific to the Saitek RAT7 mouse
    Option         "ButtonMapping"      "1 2 3 4 5 6 7 8 9 10 11 12 0 0 0 16 17 18 19 20 21"
EndSection

You'll need to restart X or reboot before this comes into effect:

sudo service kdm restart

5 - Button configuration

We'll use a program that translate pressed buttons into various actions: imwheel. It takes two parts: assigning actions to buttons, and assigning things to do to those actions.

$ sudo aptitude install imwheel

Edit the file /etc/X11/imwheel/startup.conf with the buttons you want imwheel to take care of (as root). It's not very clear but the various numbers correspond to the following actions, in order: Left, Right, Up, Down, Thumb1, Thumb2, ExtBt7, ExtBt8, ExtBt9... A zero means imwheel doesn't act on this button (it lets the default X11 action). So here it basically mean that we let X11 handle the left/right buttons as well as the wheel, but we ask ImWheel to assign button 8 to action Thumb1, button 9 to action Thumb2, button 10 to action ExtBt7, etc...

IMWHEEL_START=1
IMWHEEL_PARAMS='-b "0 0 0 0 8 9 10 11 12"'

Now configure the actions themselves in the file /etc/X11/imwheel/imwheelrc (as root):

".*"
None, Thumb1, End
None, Thumb2, Home
None, ExtBt7, Page_Down
None, ExtBt8, Page_Up
None, ExtBt9, Alt_L|Left

Here I have set the red thumb button to [Alt][Left arrow] (which goes back one page in Firefox), the thumb arrow buttons to Home/End and the thumb wheel to PageUp/PageDown so that it works in any application (which is what the 'none' means at the start of the lines).




[BsdWindowsLinux.jpg]
Windows fire

Logitech G9 Laser Mouse

Here's another simpler example with the Logitech G9 Laser Mouse whose two sideways wheel buttons don't have default functions in KDE. Those are the actions you get when you wiggle the wheel to the left or right. Some of the above points are not necessary, such as modifying the xorg.conf file.

1 - Mouse identification

$ xinput list
Logitech G9 Laser Mouse                   id=10   [slave  pointer  (2)]
Logitech G9 Laser Mouse                   id=11   [slave  pointer  (2)]

I have no idea why it shows twice...

2 - Slowing down the mouse

$ cat bin/SetMouse.sh
#!/bin/sh
xinput --set-prop 10 "Device Accel Constant Deceleration" 5
#xinput --set-prop "Logitech G9 Laser Mouse" "Device Accel Velocity Scaling" 1

In [System settings][Startup and shutdown][Autostart] I add an entry in [Script file] pointing to that script. Done.

3 - Button identification

xev shows that the 2 buttons I want are # 11 and 12.

Skip step 4

5 - Button configuration

$ sudo aptitude install imwheel

Update the file /etc/X11/imwheel/startup.conf to tell it to consider only button 11 and 12, and assign them to ExtBt7 and ExtBt8:

IMWHEEL_START=1
IMWHEEL_PARAMS='-b "0 0 0 0 0 0 11 12"'

Now configure the button actions themselves in the file /etc/X11/imwheel/imwheelrc (as root):

".*"
None, ExtBt7, Alt_L|Left
None, ExtBt8, Home

Here I have set the left wheel to [Back] in Firefox, and the right wheel to [Home].




[LinuxVsWin.jpg]
Linux vs Win

Logitech USB Laser Mouse

Now even simpler, we only do the last two steps to assign the PageUp/PageDown to the two thumb buttons:

1 - Mouse identification (optional)

$ xinput list
Logitech USB Laser Mouse                   id=8   [slave  pointer  (2)]

3 - Button identification

"xev | grep button" shows that the 2 buttons I want are #8 and 9.

5 - Button configuration

$ sudo aptitude install imwheel

Update the file /etc/X11/imwheel/startup.conf to tell it to consider only button 8 and 9, and assign them to Thumb1 and Thumb2:

IMWHEEL_START=1
IMWHEEL_PARAMS='-b "0 0 0 0 8 9"'

Now configure the button actions themselves in the file ~/.imwheelrc (as normal user):

".*"
None, Thumb1, Page_Down
None, Thumb2, Page_Up

Start imwheel (or reboot) and you are set.


Key remapping: adding a 'menu' key

In KDE you can already redifine many keys from the [System settings][Input devices][Keyboard][Advanced] menu. But there's no option there for adding a 'Menu' key.

I recently got two laptops that don't have a menu key. You know, that key which is basically identical to a right click on the mouse. Very useful for instance when spellchecking. Here's how to assign it to another key.

  1. First determine the code of the key (the keycode) you want to use:
    $ xev | grep keysym
    state 0x0, keycode 118 (keysym 0xff63, Insert), same_screen YES,
    state 0x0, keycode 118 (keysym 0xff63, Insert), same_screen YES,

    Here I pressed the Insert key and the code is, adequately, 'Insert'.
  2. The look up the keysym for the menu key, here it's simply 'Menu'.
  3. Set it up: $ echo "keysym Insert = Menu" >> ~/.Xmodmap
  4. Test it: $ xmodmap ~/.Xmodmap and now press the [Insert] key to see the right-click menu pop up.
  5. It should start on next login, but in case it doesn't, add the following to /etc/X11/xinit/xinitrc:
    if [ -f $sysmodmap ]; then
      xmodmap $sysmodmap
    fi