Python Xlib 捕获/发送鼠标点击
目前,我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上你想要 Xlib.X.ButtonPressMask | Xlib.X.ButtonReleaseMask,获取按钮按下和释放的事件(与按键按下和释放不同)。 这些事件是
ButtonPress
和ButtonRelease
,detail
实例变量为您提供按钮编号。 从收到按下事件到收到释放事件,您都知道按钮被按住。 当然,您还可以在按下某个键时接收按键事件并执行其他操作(例如退出脚本)。编辑:这个版本对我来说效果很好,例如......:
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 areButtonPress
andButtonRelease
, and thedetail
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...: