Using pyHook to get mouse coordinates to play back later
Asked Answered
H

3

2

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)
Herc answered 21/6, 2013 at 21:7 Comment(0)
F
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()
Forster answered 22/6, 2013 at 7:15 Comment(0)
R
0

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()
Rizo answered 22/6, 2013 at 7:10 Comment(3)
this sort of works for what I am trying to do. but now it repeats the two coordinates over and over 14 times before it lets another click (Granted it takes less than a second to go through the 14 cycles)Herc
Ah, I think falsetru's answer is probably closer to what you want. I didn't know how pyhook works; It would seem that putting an emulated click, "win32con.MOUSEEVENTF_LEFTDOWN" inside the event handler, would trigger the event handler "onclick" again.Rizo
This is honestly only part one of the puzzle. I know how to get it to display the co-ordinates but now I need to store them / play them back.Herc
M
0

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

    def click((x,y)):
Masaryk answered 23/12, 2013 at 17:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.