0

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)
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
user3490280
  • 21
  • 1
  • 5
  • Where does your error text come from? It looks strange, encompassing the console header and prompts. The `(187, 505)` part is just as weird. – ivan_pozdeev Aug 01 '14 at 15:43
  • I found a workaround to the issue that makes no sense. If I change the name of the callback method from onclick to onclicktwo everything works as expected???? – user3490280 Aug 01 '14 at 15:59
  • 1
    Returning True in the callback function corrected the issue – user3490280 Aug 01 '14 at 17:21
  • Post your solution as an answer and accept it. This way, the question will be marked solved and you'll get some rep and maybe more. – ivan_pozdeev Aug 01 '14 at 22:16

0 Answers0