如何使用pyhook而不是pumpMessages()来使用自己的循环?

Zac*_*ack 7 python pyhook

我正在尝试使用pyhooks来检测屏幕上任何地方的鼠标点击.问题是我只能使用PumpMessages().我希望它在我构建的while循环中运行.有没有办法实现这个/为什么它需要pumpMessages?

def onclick(event):
    print 'Mouse click!'
    return True


hm = pyHook.HookManager()

hm.MouseLeftDown = onclick

hm.HookMouse()
pythoncom.PumpMessages()
hm.UnhookMouse()
Run Code Online (Sandbox Code Playgroud)

以上是我可以让它运行的唯一方法.

我正在努力完成这样的事情:

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()
Run Code Online (Sandbox Code Playgroud)

编辑:我不是一个聪明人.场景中不需要while循环.

叹..

Mat*_*ela 14

仅供将来参考,您可以pythoncom.PumpWaitingMessages()在while循环内部使用,因为它不会锁定执行.像这样的东西:

while True:
    # your code here
    pythoncom.PumpWaitingMessages()
Run Code Online (Sandbox Code Playgroud)

  • 同意Muppet.这应该是正确的答案.谢谢你,在找到这个简单的解决方案之前,我搜索了一段时间, (3认同)

Nol*_*lty 4

来自 pyhook 教程

任何希望接收全局输入事件通知的应用程序都必须具有 Windows 消息泵。

但是,这不一定会阻止您的代码正常工作。为什么不发布您想要执行的操作,我们可以寻找一种在您的代码上下文中使用消息泵的方法。

您可能能够解决问题的一种方法是通过 PostQuitMessages(原始解决方案在这里

import ctypes
ctypes.windll.user32.PostQuitMessage(0)
Run Code Online (Sandbox Code Playgroud)