2

我正在编写一段代码来使用 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)
4

3 回答 3

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()
于 2013-06-22T07:15:01.417 回答
0

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

    def click((x,y)):
于 2013-12-23T17:20:43.260 回答
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()
于 2013-06-22T07:10:02.120 回答