7

I want to create a program that catches mouse clicks, no matter in which application it is sent to. Then it has to simulate twenty mouse clicks in one second. I am quite new to Python, and I am not really understanding much, but I've searched in several sites and I assembled this code:

import time
import ctypes
import pyHook
import pythoncom
MOUSEEVENTF_MOVE = 0x0001 # mouse move
MOUSEEVENTF_ABSOLUTE = 0x8000 # absolute move
MOUSEEVENTF_MOVEABS = MOUSEEVENTF_MOVE + MOUSEEVENTF_ABSOLUTE

MOUSEEVENTF_LEFTDOWN = 0x0002 # left button down 
MOUSEEVENTF_LEFTUP = 0x0004 # left button up 
MOUSEEVENTF_CLICK = MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP

def click(vdf):
    hm.UnhookMouse()
    ctypes.windll.user32.mouse_event(MOUSEEVENTF_CLICK, 0, 0, 0, 0)
    time.sleep(1)
    ctypes.windll.user32.mouse_event(MOUSEEVENTF_CLICK, 0, 0, 0, 0)
    hm.HookMouse()
    return 0

hm = pyHook.HookManager()
hm.SubscribeMouseAllButtonsDown(click)
hm.HookMouse()
pythoncom.PumpMessages()
os.system("pause")

This is just a sample. It has to generate 2 mouse clicks with a second interval. When I start it however, that's what comes out:

Traceback (most recent call last):
  File "C:\Documents and Settings\Valjo\Desktop\hack.py", line 3, in <module>
    import pyHook
  File "D:\Python2.7\lib\site-packages\pyHook\__init__.py", line 1, in <module>
    from HookManager import *
  File "D:\Python2.7\lib\site-packages\pyHook\HookManager.py", line 1, in<module>
    import cpyHook
  File "D:\Python2.7\lib\site-packages\pyHook\cpyHook.py", line 9, in <module>
    new_instancemethod = new.instancemethod
AttributeError: 'module' object has no attribute 'instancemethod'

And it creates some file named new.pyc...

Any ideas how to fix it? Thanks!

7
  • It looks like you're generating events in the event handler for the event that produces an infinite loop. Commented Dec 5, 2010 at 22:18
  • Oh yeah! You are right. Only I have to unhook the mouse before generating the clicks then hook it again! Thanks a lot! Commented Dec 13, 2010 at 10:10
  • 1
    It seems one of your files is named new.py and it shadows new module from Python's stdlib. Write in your script: import new; print new.__file__. What is the output? Commented Dec 13, 2010 at 13:57
  • None. It doesn't print anything! Commented Dec 13, 2010 at 16:21
  • Maybe it has something to deal with the fact that the event handling function holds one argument, but never uses it? Or it is a module error? Commented Dec 14, 2010 at 8:48

1 Answer 1

1

Use eventghost: http://www.eventghost.org/

  • Open source
  • You can write plugins in Python
  • You can catch lots of different events (you can even capture raw HID devices)
  • You can make it run/do anything you could normally do with Python
Sign up to request clarification or add additional context in comments.

1 Comment

I don't really need this. J.F. Sebastien already helped me to fix my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.