How to use the "search" button as middle mouse button
- without any userspace tools!

The problem

Several people have a Logitech MX Revolution mouse. While the mouse is quite useful it has one major disadvantage: It does not have a "real" middle mouse button.

There's a wheel and you can click it. The wheel button has the id 6 (not 2) but that could be corrected with an appropriate button mapping. However, clicking the wheel often results in some minor scrolling as well. Usually, you don't want that but it's hard to avoid it.

There is a button behind the wheel which would be perfect as a middle mouse button replacement. Alas, that button is hardwired to produce a keyboard event (XF86Search). You cannot make it to produce mouse events by normal means.

Other approaches

Searching the web, you quickly come over across a lot of people with the same problem and idea: They all want to use the search button (the one behind the wheel) as middle mouse button.

The approach you usually find is to capture the keyboard event with a hotkey management software like xbindkeys. Then you define an action that will inject a middle mouse click in the X11 event queue (i.e. by calling 'xte' from the xautomation package or using xmacroplay).

Disadvantages of these approaches

Because these approaches work on top of X11, they fail whenever X11 is not involved. In text mode, in SDL or framebuffer applications and under other certain circumstances, the search button will not work as intended. It will still produce a keyboard event.

My solution: Use the kernel's Mac support

To come to this idea, I had to get a more abstract picture of the real problem in my head. My problem was not that I have a MX revolution, my problem was that I do not have a middle mouse button. Who else has no mmb? Macs do! And when I went to search for how they deal with it when running Linux, I discovered that the kernel can emulate the buttons 2 and 3 by keyboard events (which is kind of logical as the keyboard is the only other input device that's always present).

So I just recompiled my kernel with the Mac HID driver enabled

 CONFIG_MACINTOSH_DRIVERS=y
 CONFIG_MAC_EMUMOUSEBTN=y

and added these lines to my /etc/sysctl.conf:

 dev/mac_hid/mouse_button_emulation = 1
 dev/mac_hid/mouse_button2_keycode = 217
 dev/mac_hid/mouse_button3_keycode = 999

The keycode for button 2 was determined with 'showkey' and since I don't need to emulate a third (right) mouse button, I set the code to something that doesn't exist.

I just had to reboot and now have a working middle mouse button! And it's all done on kernel level, meaning it works in text mode, SDL and FB apps and of course X11. No user space tools required.