I am looking to create an application that in PyHook to help record mouse click positions. At a high level I want to
- Bind mouse-clicks to a method or function
- When the user clicks the mouse, unhook the mouse clicks
- Do some stuff in my program and at a later time re-bind the mouse (hookMouse) and wait for another click
Use of pumpMessages blocks so was not an option. If I knew how to send a quit message to pumpMessages this may be an alternative. I attempted to use pythoncom.PumpWaitingMessages() which seem to be a perfect ft, but causes an exception each time the mouse is clicked. I have been unable to debug why.
def onclick(event):
global hm
try:
print (str(event.Position))
hm.UnhookMouse()
except:
pass
# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.SubscribeMouseLeftDown(onclick)
# set the hook
hm.HookMouse()
# wait forever
pythoncom.PumpWaitingMessages()
The error, precisely as it is seen:
Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
>>> (187, 505)
Traceback (most recent call last):
** IDLE Internal Exception:
File "C:\Python34\lib\idlelib\run.py", line 121, in main
seq, request = rpc.request_queue.get(block=True, timeout=0.05)
File "C:\Python34\lib\queue.py", line 175, in get
raise Empty
queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
** IDLE Internal Exception:
File "C:\Python34\lib\idlelib\run.py", line 121, in main
seq, request = rpc.request_queue.get(block=True, timeout=0.05)
File "C:\Python34\lib\queue.py", line 176, in get
self.not_empty.wait(remaining)
File "C:\Python34\lib\threading.py", line 294, in wait
gotit = waiter.acquire(True, timeout)
TypeError: an integer is required (got type NoneType)