Skip to content

Key buttons should be written to keyboard event device. Writing to new input device causes severe lag. #14

@jeffbdavenport

Description

@jeffbdavenport

If you map a mouse button to, let's say shift. Then you repeatedly press the shift button on your mouse while typing. There is severe lag you'll see. This is because the kernel is having to do extra work to push the new input device events. If remappings are keyboard keys, they should be written directly to the keyboard event device to eliminate the overhead.

As an example, I was able to eliminate the lag by writing this specific python script to map my buttons to shift and ctrl:

#!/usr/bin/env python3
import os
from evdev import InputDevice, ecodes as e
import select
import time

m = os.popen('cat /proc/bus/input/devices|grep -v "Control"|grep "2.4G Mouse" -A 4|tail -1|egrep -o "event[0-9]+"').read()
k = os.popen('cat /proc/bus/input/devices|grep "keyboard" -A 4|tail -1|egrep -o "event[0-9]+"').read()
# Input event devices listed in my /proc/bus/input/devices
MOUSE_DEVICE = f"/dev/input/{m.strip()}"
KEYBOARD_DEVICE = f"/dev/input/{k.strip()}"
#MOUSE_DEVICE = f"/dev/input/event7"
#KEYBOARD_DEVICE = f"/dev/input/event3"

mouse = InputDevice(MOUSE_DEVICE)
kb = InputDevice(KEYBOARD_DEVICE)

print("Remapping started... Press Ctrl+C to stop.")
while True:
    r, w, x = select.select([mouse.fd], [], [], 0.01)  # Timeout of 0.01s to avoid CPU hogging
    if mouse.fd in r:
        for event in mouse.read():
            if event.type == e.EV_KEY:
                if event.code == e.BTN_EXTRA:  # Button 9 -> Shift
                    kb.write(e.EV_KEY, e.KEY_LEFTSHIFT, event.value)
                elif event.code == e.BTN_SIDE:  # Button 8 -> Ctrl
                    kb.write(e.EV_KEY, e.KEY_LEFTCTRL, event.value)
    time.sleep(0.1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions