python - [Python][evdev] 获取鼠标点击后光标位置

标签 python linux mouseevent evdev

我遇到一个问题,需要捕获来自鼠标的所有事件。浏览网页后,我读到了 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/

相关文章:

python - 为什么损失函数在第一个纪元之后总是返回零?

python - izip 的示例代码如何工作?

python - 使用 Pandas Dataframes 根据间隙长度计算事件日期

java - 在 Windows 7 中运行进程并获取其 ID

c++ - 使用 XFRM 消息更新 IPsec key

python - 在创建表时执行 Django fixtures

linux - GTK-打印 : detect if printer is configured/printing would work?

javascript - 使用 jQuery 和 CSS3 自定义圆形光标

svg - Elm:向 SVG 元素添加点击事件不起作用——这可能吗?

javascript - 当鼠标在 Rect 中按下时,如何连续触发回调?