Using pyHook to get mouse coordinates to play back later

后端 未结 3 1933
走了就别回头了
走了就别回头了 2021-01-23 06:00

I am writing a chunk of code to get gather mouse click information using pyHook and then the win32api to get access to a click function. Essentially I am trying to use the mouse

相关标签:
3条回答
  • 2021-01-23 06:17

    hm.SubscribeMouseAllButtonsDown(click) -> hm.SubscribeMouseAllButtonsDown(onclick)

    Removed click() call in onclick.

    import win32api, win32con, time, win32ui, pyHook, pythoncom
    
    def click(x,y):
        win32api.SetCursorPos((x,y))
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
    
    def onclick(event):
        print event.Position
        return True
    
    hm = pyHook.HookManager()
    hm.SubscribeMouseAllButtonsDown(onclick)
    hm.HookMouse()
    pythoncom.PumpMessages()
    hm.UnhookMouse()
    
    0 讨论(0)
  • 2021-01-23 06:25

    I can't install pyhook, so this is a stab in the dark. I've assumed (event_x, event_y) = event.Position.

    import win32api, win32con, time, win32ui, pyHook, pythoncom
    
    #Define the clicks in the win32api
    def click(x,y):
        win32api.SetCursorPos((x,y))
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
    
    def onclick(event):
        print event.Position
        (event_x, event_y) = event.Position
        click(event_x, event_y)
        return True
    
    hm = pyHook.HookManager()
    hm.SubscribeMouseAllButtonsDown(onclick)
    hm.HookMouse()
    pythoncom.PumpMessages()
    hm.UnhookMouse()
    
    0 讨论(0)
  • 2021-01-23 06:35

    click() gets 2 parameters and you are passing a tuple (event.position is a tuple). Do instead:

        def click((x,y)):
    
    0 讨论(0)
提交回复
热议问题