Using pyHook to get mouse coordinates to play back later

二次信任 提交于 2019-12-02 04:51:03

问题


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 to record a pattern of clicks to be recorded and played back later.

Here is my present code:

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):
    click()
    print event.Position
    return True

hm = pyHook.HookManager()
hm.SubscribeMouseAllButtonsDown(click)
hm.HookMouse()
pythoncom.PumpMessages()
hm.UnhookMouse()

I am sure there is something stupidly simple.

Also here is the debug I got from running this:

Traceback (most recent call last):
File "C:\Python27\lib\site-packages\pyHook\HookManager.py", line 325, in MouseSwitch
return func(event)
TypeError: click() takes exactly 2 arguments (1 given)

回答1:


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



回答2:


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



回答3:


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

    def click((x,y)):


来源:https://stackoverflow.com/questions/17244317/using-pyhook-to-get-mouse-coordinates-to-play-back-later

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!