使用pyHook获取鼠标坐标以便稍后播放

时间:2013-06-21 21:07:21

标签: python pyhook

我正在编写一大块代码来使用pyHook收集鼠标点击信息,然后使用win32api来访问点击功能。基本上我正在尝试使用鼠标来记录要记录并稍后播放的点击模式。

这是我现在的代码:

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

我确信有些事情很简单。

这也是我从运行它得到的调试:

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)

3 个答案:

答案 0 :(得分:1)

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

click()删除了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()

答案 1 :(得分:0)

我无法安装pyhook,所以这是在黑暗中刺伤。我假设(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()

答案 2 :(得分:0)

click()获取2个参数,你传递一个元组(event.position是一个元组)。改为:

    def click((x,y)):