我遇到一个问题,需要捕获来自鼠标的所有事件。浏览网页后,我读到了 evdev 模块并尝试了一下。
我现在有一个脚本,可以从鼠标获取所有事件,以防止与其他窗口的其他交互(我最初问题中的重要点)。 有了它,我可以在单击按钮和移动鼠标时读取事件。但我没有找到如何在单击按钮时获取光标位置。
#!/usr/bin/env python
# -*- coding: utf8 -*-
from evdev import InputDevice, categorize, ecodes
from os import listdir
from os.path import isfile, isdir, exists, join
def my_list():
print('*** my_list(): begin.')
devices = map(InputDevice, list_devices())
for dev in devices:
print('%-20s %-32s %s' % (dev.fn, dev.name, dev.phys))
print('*** my_list(): end.')
def monitor_device(dev):
for event in dev.read_loop():
if event.type == ecodes.EV_KEY:
print(categorize(event))
print(event)
if __name__ == "__main__" :
dev_path = '/dev/input/event17'
if(exists(dev_path)):
device = InputDevice(dev_path)
try:
device.grab()
monitor_device(device)
except KeyboardInterrupt :
device.ungrab()
print('User aborted the program.')
如何使用 evdev 做到这一点?如果我不能,还有其他方法吗?
任何帮助将不胜感激。 :)
最佳答案
您可以监听 ABS_MT_POSITION_X 和 ABS_MT_POSITION_Y 事件,并将值存储在 x、y 变量中。然后,当您收到 BTN_TOUCH 事件时,您就知道位置是什么。
关于python - [Python][evdev] 获取鼠标点击后光标位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17996490/