3

I'm trying to remap my mouse buttons, two side ones and middle click to 4,5,6 keys on my keyboard,found some guides but I'm very new to Ubuntu and linux in general, I tried to do that for a few days now but to no avail.. Could someone guide me through that or point me to some comprehensive guide that a noob could follow? Any help will be appreciated.

M R
  • 31

2 Answers2

14

There are basically two approaches to this problem:

  • Bind an action to the mouse button and fake a key press

or

  • Map the mouse button's scancode to a different keycode

The latter only works if the input driver reports a scancode for the specific button. All mice I own report scancodes for all buttons except the scroll wheel.

I personally prefer remapping the scancode, so I'll only explain that method.

Remapping mouse button's scancode to different keycode

This is more or less the same procedure as I described here.

Run sudo evtest (you may have to install evtest first) and select your mouse. Press the buttons you want to remap (if you don't see any output, press Ctrl+C and repeat the process until you found your mouse). The output should contain lines like the following:

Event: time 1558613958.149431, type 4 (EV_MSC), code 4 (MSC_SCAN), value 90004
Event: time 1558613958.149431, type 1 (EV_KEY), code 275 (BTN_SIDE), value 1
Event: time 1558613958.149431, -------------- SYN_REPORT ------------

The first line tells us the scancode (in my case 90004), the second line reports the currently mapped keycode (in my case BTN_SIDE). Write down the scancodes of all mouse buttons you want to remap.

Now run the following command, where eventX is the one you chose when using evtest:

grep "" /sys/class/input/eventX/device/id/*

This tells you bustype, vendor, product and version of your mouse. In my case, the output is:

/sys/class/input/event7/device/id/bustype:0003
/sys/class/input/event7/device/id/product:4102
/sys/class/input/event7/device/id/vendor:062a
/sys/class/input/event7/device/id/version:0110

Now create the following file:

/etc/udev/hwdb.d/99-mouse-remap.hwdb

evdev:input:b[bustype]v[vendor]p[product]e[version]*
 ID_INPUT_KEY=1
 KEYBOARD_KEY_[scancode]=[desired-key]
 KEYBOARD_KEY_[scancode]=[desired-key]
 KEYBOARD_KEY_[scancode]=[desired-key]

...where

  • [bustype], [vendor], [product] and [version] are the ones obtained in the previous step with letters converted to uppercase
  • [scancode] are the ones from the first step with letters converted to lowercase
  • [desired-key] are your desired keys to fire, e.g., 4,5 and 6
  • the evdev:... line has no preceding space
  • the KEYBOARD_KEY... lines have exactly one preceding space
  • ID_INPUT_KEY=1 stays constant

The file would look like this for my mouse:

evdev:input:b0003v062Ap4102e0110*
 ID_INPUT_KEY=1
 KEYBOARD_KEY_90003=4
 KEYBOARD_KEY_90004=5
 KEYBOARD_KEY_90005=6

Now run sudo systemd-hwdb update and reboot (or run sudo udevadm control --reload-rules && sudo udevadm trigger).

F1iX
  • 163
danzel
  • 6,044
  • I wish I could give this more votes. Thank you so so so much. – Cody Nov 27 '19 at 20:23
  • What about wheel up and wheel down ? – Zulgrib Jan 10 '20 at 23:18
  • 2
    @Zulgrib the mouse wheel is usually reported as a relative axis by the kernel input driver. The conversion to virtual mouse buttons (and generating press/release events) happens at a later stage and can't be achieved by a simple remapping. You can however take a look at the output of evtest. If your mouse wheel generates key events and scancodes, you can remap it using this method. If not, you could try xbindkeys in combination with xdotools (if you are running Xorg). As a last resort, you can try to achieve this with a python script using python-evdev. – danzel Jan 11 '20 at 10:10
  • 1
    Just wanted to thank you for your perfectly detailed answer. Just remapped four buttons on my new Evoluent VerticalMouse. It's working like a charm. – TheMSG Oct 13 '22 at 16:13
  • Thanks a lot for your post! I just created a script to capture the keyboard events and send MIDI messages, e.g., to use a mouse for strobe in QLC+ https://github.com/F1iX/midi-mouse – F1iX Jun 17 '23 at 13:00
0

I don't understand. I remapped my mouse keybinds to have ctrl and shift on the mouse. Works perfectly, however, when i press M1 and shift, shift is ignored. But not ctrl when pressing ctrl and M1. What could have gone wrong ?