Python Xlib 捕获/发送鼠标点击

发布于 2024-07-27 13:15:37 字数 811 浏览 8 评论 0原文

目前,我正在尝试使用 Python 来检测何时按住鼠标左键,然后开始快速发送此事件,而不是只发送一次。 我基本上想做的是,当按住鼠标左键时,它会不断点击,直到您松开为止。 但我对整个 Xlib 有点困惑,我认为它实际上非常令人困惑。 任何关于如何做到这一点的帮助都会非常棒。 这就是我到目前为止所得到的:

#!/usr/bin/env python

import Xlib
import Xlib.display

def main():
    display = Xlib.display.Display()
    root = display.screen().root
    while True:
        event = root.display.next_event()
        print event

if __name__ == "__main__":
    main()

但不幸的是控制台中没有输出。 在互联网上快速搜索后,我发现了以下内容:

root.change_attributes(event_mask=Xlib.X.KeyPressMask)
root.grab_key(keycode, Xlib.X.AnyModifier, 1, Xlib.X.GrabModeAsync,
              Xlib.X.GrabModeAsync)

这似乎对于使用给定的键码捕获特殊事件很重要。 但首先,鼠标左键单击有什么键码(如果有的话)? 其次,我如何检测它何时被按下,然后开始快速发送鼠标单击事件。 我将非常感谢您的帮助。 (也许用热键停止这个脚本的方法也很酷......)

At the moment I'm trying to use Python to detect when the left mouse button is being held and then start to rapidly send this event instead of only once. What I basically want to do is that when the left mouse button is held it clicks and clicks again until you let it go. But I'm a bit puzzled with the whole Xlib, I think it's very confusing actually. Any help on how to do this would be really awesome. That's what I've got so far:

#!/usr/bin/env python

import Xlib
import Xlib.display

def main():
    display = Xlib.display.Display()
    root = display.screen().root
    while True:
        event = root.display.next_event()
        print event

if __name__ == "__main__":
    main()

But there is unfortunately no output in the console. After a quick search on the internet I found the following:

root.change_attributes(event_mask=Xlib.X.KeyPressMask)
root.grab_key(keycode, Xlib.X.AnyModifier, 1, Xlib.X.GrabModeAsync,
              Xlib.X.GrabModeAsync)

This is seemingly import to catch a special event with the given keycode. But firstly what keycode does the left-mouse click have, if any at all? And secondly how can I detect when it is being held down and then start sending the mouseclick event rapidly. I would be really grateful for help. (Maybe a way to stop this script with a hotkey would be cool aswell...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野味少女 2024-08-03 13:15:37

实际上你想要 Xlib.X.ButtonPressMask | Xlib.X.ButtonReleaseMask,获取按钮按下和释放的事件(与按键按下和释放不同)。 这些事件是 ButtonPressButtonReleasedetail 实例变量为您提供按钮编号。 从收到按下事件到收到释放事件,您都知道按钮被按住。 当然,您还可以在按下某个键时接收按键事件并执行其他操作(例如退出脚本)。

编辑:这个版本对我来说效果很好,例如......:

import Xlib
import Xlib.display

def main():
    display = Xlib.display.Display(':0')
    root = display.screen().root
    root.change_attributes(event_mask=
        Xlib.X.ButtonPressMask | Xlib.X.ButtonReleaseMask)

    while True:
        event = root.display.next_event()
        print event

if __name__ == "__main__":
    main()

Actually you want Xlib.X.ButtonPressMask | Xlib.X.ButtonReleaseMask, to get events for button presses and releases (different from key presses and releases). The events are ButtonPress and ButtonRelease, and the detail instance variable gives you the button number. From when you get the press event, to when you get the release event, you know the button is being held down. Of course you can also receive key events and do something else (e.g. exit your script) when a certain key is pressed.

Edit: this version works fine for me, for example...:

import Xlib
import Xlib.display

def main():
    display = Xlib.display.Display(':0')
    root = display.screen().root
    root.change_attributes(event_mask=
        Xlib.X.ButtonPressMask | Xlib.X.ButtonReleaseMask)

    while True:
        event = root.display.next_event()
        print event

if __name__ == "__main__":
    main()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文