Askubuntu帖子包含一个答案,我将在下面总结。
问题是xbindkeys会抓住整个鼠标,从而使修饰符+鼠标单击映射变得不确定。答案通过python-uinput脚本使用uinput 来监视/dev/my-mouse
拇指按钮的单击并将Ctrl键发送到虚拟键盘。详细步骤如下:
1.制定udev规则
对于鼠标,文件/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将寻找名称类似于event5的内核设备。SYMLINK用于找到/dev/my_mx_mouse
该组可读的鼠标mxgrabber
。
要查找硬件信息,请运行以下命令:
udevadm info -a -n /dev/input/eventX
对于uinput,文件/etc/udev/rules.d/94-mxkey.rules
:
KERNEL=="uinput", GROUP="mxgrabber", MODE="660"
拔下并插入鼠标,或者使用强制udev触发规则udevadm trigger
。
2.激活UINPUT模块
sudo modprobe uinput
在/etc/modules-load.d/uinput.conf
:
uinput
3.建立新群组
sudo groupadd mxgrabber
sudo usermod -aG mxgrabber your_login
4. Python脚本
安装python-uinput library
和python-evdev library
。
下面的脚本需要标识按钮的event.code:
#!/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.整理
使python文件可执行,并确保在启动时加载该文件。