1

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.

  • Do you really want the buttons to produce the characters 4,5,6 or do you want to map them to keyboard shortcuts? – danzel May 22 '19 at 1:53
  • I really want the buttons to produce the characters 4,5,6. – M R May 23 '19 at 10:12
3

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]=4
 KEYBOARD_KEY_[scancode]=5
 KEYBOARD_KEY_[scancode]=6

...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
  • the evdev:... line has no preceding space
  • the KEYBOARD_KEY... lines have exactly one preceding space

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.

  • 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 at 23:18
  • 1
    @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 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.