如何在linux中捕获和修改鼠标事件?
How to capture and modify mouse events in linux?
我想编写一个程序,在按下特定键时改变鼠标的行为。但是我对linux.
中的事件机制不是很熟悉
我的猜测是我需要通过“事件队列”进行过滤以查找“按键”和“鼠标按下”事件,并在传递鼠标事件之前以某种方式修改鼠标事件或丢弃它并创建一个新事件.
如何使用 C++/Python 完成此操作?我应该使用哪些工具或库?
SDL2 是一个很棒的硬件接口库,它很可能随您的发行版一起安装。
This tutorial 讨论了如何使用鼠标状态,我已经用它来整体学习 SDL2。
SDL2 文档也很不错:https://wiki.libsdl.org/SDL_GetMouseState
我的问题已通过 python-evdev
解决
原来很简单。只需抓住鼠标并创建另一个 uinput 来写入所需的事件。
import evdev
from evdev import ecodes
key=ecodes.KEY_LEFTSHIFT
kb=evdev.InputDevice('/dev/input/event1') # keybord
mouse=evdev.InputDevice('/dev/input/event3') # mouse
dummy=evdev.UInput.from_device(mouse)
hwheel=evdev.UInput({ecodes.EV_REL:[ecodes.REL_HWHEEL]})
mouse.grab()
for event in mouse.read_loop():
if event.type==ecodes.EV_REL and event.code==ecodes.REL_WHEEL and key in kb.active_keys():
hwheel.write(ecodes.EV_REL, ecodes.REL_HWHEEL, event.value)
hwheel.write(ecodes.EV_SYN, ecodes.SYN_REPORT, 0)
else:
dummy.write_event(event)
我想编写一个程序,在按下特定键时改变鼠标的行为。但是我对linux.
中的事件机制不是很熟悉我的猜测是我需要通过“事件队列”进行过滤以查找“按键”和“鼠标按下”事件,并在传递鼠标事件之前以某种方式修改鼠标事件或丢弃它并创建一个新事件.
如何使用 C++/Python 完成此操作?我应该使用哪些工具或库?
SDL2 是一个很棒的硬件接口库,它很可能随您的发行版一起安装。
This tutorial 讨论了如何使用鼠标状态,我已经用它来整体学习 SDL2。
SDL2 文档也很不错:https://wiki.libsdl.org/SDL_GetMouseState
我的问题已通过 python-evdev
解决原来很简单。只需抓住鼠标并创建另一个 uinput 来写入所需的事件。
import evdev
from evdev import ecodes
key=ecodes.KEY_LEFTSHIFT
kb=evdev.InputDevice('/dev/input/event1') # keybord
mouse=evdev.InputDevice('/dev/input/event3') # mouse
dummy=evdev.UInput.from_device(mouse)
hwheel=evdev.UInput({ecodes.EV_REL:[ecodes.REL_HWHEEL]})
mouse.grab()
for event in mouse.read_loop():
if event.type==ecodes.EV_REL and event.code==ecodes.REL_WHEEL and key in kb.active_keys():
hwheel.write(ecodes.EV_REL, ecodes.REL_HWHEEL, event.value)
hwheel.write(ecodes.EV_SYN, ecodes.SYN_REPORT, 0)
else:
dummy.write_event(event)