[英]How do I use my own loop with pyhook instead of pumpMessages()?
我正在尝试使用 pyhooks 来检测屏幕上任意位置的鼠标点击。 问题是我只能让它与 PumpMessages() 一起工作。 我希望它在我构建的 while 循环内运行。 有没有办法完成这个/为什么需要 pumpMessages?
def onclick(event):
print 'Mouse click!'
return True
hm = pyHook.HookManager()
hm.MouseLeftDown = onclick
hm.HookMouse()
pythoncom.PumpMessages()
hm.UnhookMouse()
以上是我让它运行的唯一方法。
我正在尝试完成这样的事情:
sTime = time.time()
def onclick(event):
global sTime
print 'Time between clicks equals: %i' % time.time() - stime
sTime = time.time()
return True
hm.MouseLeftDown = OnClick
while True:
hm.HookMouse()
编辑:我不是一个聪明人。 场景中不需要 while 循环..
叹..
仅供将来参考,您可以在 while 循环内使用pythoncom.PumpWaitingMessages()
,因为它不会锁定执行。 像这样:
while True:
# your code here
pythoncom.PumpWaitingMessages()
任何希望接收全局输入事件通知的应用程序都必须有一个 Windows 消息泵。
但是,这不一定会阻止您的代码正常工作。 你为什么不发布你正在尝试做的事情,我们可以寻找一种在你的代码上下文中使用消息泵的方法。
您可能能够解决问题的一种方法是通过 PostQuitMessages( 此处为原始解决方案)
import ctypes
ctypes.windll.user32.PostQuitMessage(0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.