xdotool如何映射修改器( 比如 CTRL ) 使用xbindkeys鼠标按钮

分享于 

6分钟阅读

ubuntu

  繁體

方法A:

用以下命令更新.xbindkeysrc文件:


"xdotool keydown ctrl"


 b:8



"xdotool keyup ctrl"


 release + b:8



这是我最初尝试的,但它有副作用。

方法B:

用以下命令更新.xbindkeysrc.scm文件:


(xbindkey '("b:8")"xdotool keydown ctrl")


(xbindkey '(release"b:8")"xdotool keyup ctrl")



(xbindkey '("m:0x14""b:8")"xdotool keydown ctrl")


(xbindkey '(release"m:0x14""b:8")"xdotool keyup ctrl")



它只能部分地工作。

方法C:

用以下命令更新.xbindkeysrc文件:


"xdotool keydown ctrl"


 b:8



"xdotool keyup ctrl"


 release + control + b:8



xev | grep buttonxbindkeys -v

xev记录的普通鼠标单击应该类似于:


state 0x10, button 1, same_screen YES


state 0x110, button 1, same_screen YES




state 0x10, button 8, same_screen YES


state 0x10, button 8, same_screen YES



但是,当启用上面的xbindkeys配置时,它不会记录内容,尽管thumb按钮是有意义的,因为它映射到CTRL,因此不再是鼠标按钮了,奇怪的是button 1也没有记录,这可能是因为xbindkeys不执行它,但本身正在识别它:


Button press !


e.xbutton.button=8


e.xbutton.state=16


"xdotool keydown ctrl"


 m:0x0 + b:8 (mouse)


got screen 0 for window 16d


Start program with fork+exec call


Button press !


e.xbutton.button=1


e.xbutton.state=20


Button release !


e.xbutton.button=1


e.xbutton.state=276


Button release !


e.xbutton.button=8


e.xbutton.state=20


"xdotool keyup ctrl"


 Release + m:0x4 + b:8 (mouse)


got screen 0 for window 16d


Start program with fork+exec call



方法D:

用以下命令更新.xbindkeysrc文件:


"xdotool keydown ctrl"


 b:8



"xdotool keyup ctrl"


 release + control + b:8



"xdotool click 1"


 b:1



简单,但会导致无限循环的点击。


我最终编写了一个脚本,用于监视/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"



要查找有关硬件的信息,应运行类似


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



对于uinput:


/etc/udev/rules.d/94-mxkey.rules


----------------------------------------------------


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



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库和python-evdev库。

脚本非常简单,识别你的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文件设为可执行文件,并要求桌面管理器在启动时加载该文件。


按钮  MODI  buttons  鼠标  Thumb  CTRL  
相关文章