Linux – How to PROPERLY map a keyboard key to a mouse button

keyboardlinuxmousexbindkeysxorg

Question summary: I want one of my mouse buttons to be registered as the left Windows button Super_L by X11.


In my window manager, I can move windows around by holding the "left Windows button" (Left Super) and dragging a window with the left mouse button. I want to be able to do that without touching the keyboard, so I want to map the left Super key to mouse button 11, that way I can hold mouse button 11 and click+drag windows.

The most obvious solution is using xbindkeys and xte like this (.xbindkeysrc):

"xte 'keydown Super_L'"
  b:11

"xte 'keyup Super_L'"
  b:11 + release

This works like this:

  • When I press down mouse button 11, Super_L is also pressed down
  • When I release mouse button 11, Super_L is also released

But there's a problem: I can't move windows using Super_L + Mouse1 if I'm also holding down another mouse button, like Mouse button 11. Using the solution above, mouse button 11 is still being registered as pressed and released, and so none of the window manager operations work.

I have tried this using both Cinnamon and Awesome WM, and absolutely none of the Super_L keyboard combinations work while mouse button 10 or 11 is being held down.

A subpar hack

I'm currently working around this issue by causing the mouse 11 click to hold the Super_L button for a certain amount of time. That way I can click the mouse button, then drag stuff around for a brief period afterwards:

"xte 'keydown Super_L' 'usleep 250000' 'keyup Super_L'"
  b:11

Another attempt

As suggested by totti, I tried this xbindkeys configuration:

"xte 'mouseup 10' 'keydown Super_L'"
  b:10

"xte 'keyup Super_L'"
  b:10 + Release

It doesn't work. It seems the Super_L key is being held down, because as soon as I release button 10 it remains held down for ever (until I press the Super_L key again on the keyboard) but the mouse button is still being registered, because I can't click&drag windows. I don't think I'm going to be able to make this work using xbindkeys and xte.

Best Answer

An askubuntu post contains an answer that I will summarize below.

The problem is that xbindkeys grabs the entire mouse, making modifers+mouse click mapping uncertain. The answer uses uinput via python-uinput script to monitor /dev/my-mouse for the thumb button click and send the Ctrl key to the virtual keyboard. Here are the detailed steps:

1. Make udev rules

For the mouse, file /etc/udev/rules.d/93-mxmouse.conf.rules :

KERNEL=="event[0-9]*", SUBSYSTEM=="input", SUBSYSTEMS=="input", 
ATTRS{name}=="Logitech Performance MX", SYMLINK+="my_mx_mouse", 
GROUP="mxgrabber", MODE="640"

Udev will look for kernel devices with names like event5. The SYMLINK is for finding the mouse in /dev/my_mx_mouse, readable by the group mxgrabber.

To find hardware information run something like :

udevadm info -a -n /dev/input/eventX

For uinput, file /etc/udev/rules.d/94-mxkey.rules :

KERNEL=="uinput", GROUP="mxgrabber", MODE="660"

Unplug and plug your mouse, or force udev to trigger the rules with udevadm trigger.

2. Activate UINPUT Module

sudo modprobe uinput

And in /etc/modules-load.d/uinput.conf :

uinput

3. Create new group

sudo groupadd mxgrabber
sudo usermod -aG mxgrabber your_login

4. Python script

Install python-uinput library and python-evdev library.

The script below requires identifying the event.code of the button :

#!/usr/bin/python3.5
# -*- coding: utf-8 -*-

"""
Sort of mini driver.
Read a specific InputDevice (my_mx_mouse),
monitoring for special thumb button
Use uinput (virtual driver) to create a mini keyboard
Send ctrl keystroke on that keyboard
"""

from evdev import InputDevice, categorize, ecodes
import uinput

# Initialize keyboard, choosing used keys
ctrl_keyboard = uinput.Device([
    uinput.KEY_KEYBOARD,
    uinput.KEY_LEFTCTRL,
    uinput.KEY_F4,
    ])

# Sort of initialization click (not sure if mandatory)
# ( "I'm-a-keyboard key" )
ctrl_keyboard.emit_click(uinput.KEY_KEYBOARD)

# Useful to list input devices
#for i in range(0,15):
#    dev = InputDevice('/dev/input/event{}'.format(i))
#    print(dev)

# Declare device patch.
# I made a udev rule to assure it's always the same name
dev = InputDevice('/dev/my_mx_mouse')
#print(dev)
ctrlkey_on = False

# Infinite monitoring loop
for event in dev.read_loop():
    # My thumb button code (use "print(event)" to find)
    if event.code == 280 :
        # Button status, 1 is down, 0 is up
        if event.value == 1:
            ctrl_keyboard.emit(uinput.KEY_LEFTCTRL, 1)
            ctrlkey_on = True
        elif event.value == 0:
            ctrl_keyboard.emit(uinput.KEY_LEFTCTRL, 0)
            ctrlkey_on = False

5. Finishing

Make the python file executable and ensure it is loaded at startup.

Related Question