Thanks for reply!
My code is well formated at 
https://stackoverflow.com/questions/55399565/hook-the-left-mouse-button-down-event-on-any-window
or the following :


import win32gui
import win32ui
import win32con


def onMousePressed(self):
    print('onMousePressed', win32gui.GetCursorPos())


def listener():
    windowHandle = win32gui.WindowFromPoint(win32gui.GetCursorPos())
    clickedWindow = win32ui.CreateWindowFromHandle(windowHandle)
    clickedWindow.HookMessage(onMousePressed, win32con.WM_LBUTTONDOWN)
    # print('-------------registerMouseEvent', clickedWindow)


while True:
    listener()
    time.sleep(8)


what is wrong and what's the good practice ?  Thanks so much !




在2019年03月30 03时35分, "Tim Roberts"<t...@probo.com>写道:

Zhao Lee wrote:
> I originally posted the question here
> <https://stackoverflow.com/questions/55399565/hook-the-left-mouse-button-down-event-on-any-window>,
> please help ,thank you !
>
>
> I want to hook the left mouse button down event on any window, my code
> as following :
> |importwin32guiimportwin32uiimportwin32condefonMousePressed(self):print('onMousePressed',win32gui.GetCursorPos())deflistener():
>  
>  windowHandle =win32gui.WindowFromPoint(win32gui.GetCursorPos()) 
>  clickedWindow =win32ui.CreateWindowFromHandle(windowHandle) 
>  clickedWindow.HookMessage(onMousePressed,win32con.WM_LBUTTONDOWN)#
> print('-------------registerMouseEvent', clickedWindow)whileTrue: 
>  listener()|
>
> However , the |onMousePressed| function was never called when clicked,
> what is wrong ?
>
> P.S. I know some similar projects such as PyUserInput
> <https://github.com/PyUserInput/PyUserInput>, mouse
> <https://github.com/boppreh/mouse>, pynput
> <https://pythonhosted.org/pynput/keyboard.html#monitoring-the-keyboard>,
> just want to know why my code didn't work.
>
Your code came across badly formatted, so I can't tell exactly what you
wrote.  You aren't actually calling listener() from within a continuous,
tight CPU loop, are you?  This function is going to get called thousands
and thousands of times a second. This is very poor practice, and it's
part of the reason these other modules exist.

You attach to an external window, install your mouse message hook, then
your function exits, the window object is deleted, and the hook is released.

--
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.


_______________________________________________
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32

Reply via email to