How to use native loop with pyhook instead of pumpMessages ()?

I am trying to use pyhooks to detect mouse clicks anywhere on the screen. The problem is that I can get it to work with PumpMessages (). I would like it to work inside the while loop that I created. Is there a way to accomplish this / why does he need pump messages?

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


hm = pyHook.HookManager()

hm.MouseLeftDown = onclick

hm.HookMouse()
pythoncom.PumpMessages()
hm.UnhookMouse()

This is the only way to make it work.

I am trying to execute something like this:

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()

EDIT: I'm not a smart person. There is no need for a while loop in the script.

Sigh..

+5
source share
2 answers

From the pyhook tutorial :

Any application that wants to receive global event input notifications must have a Windows message pump.

. , , .

- PostQuitMessages ( )

import ctypes
ctypes.windll.user32.PostQuitMessage(0)
+3

pythoncom.PumpWaitingMessages() while, . - :

while True:
    # your code here
    pythoncom.PumpWaitingMessages()
+13

All Articles