467,122 Members | 1,264 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,122 developers. It's quick & easy.

mouse control with python

Is there a way I can control the mouse with python? I need to be able to
navigate and click in other applications based on vectors sent from my
program. Basically a mouse macro type thing.

any help?
Jul 18 '05 #1
  • viewed: 14330
1 Reply
Very nice. Very helpful! Thank you!!

I ran around looking at ctypes and the User32 dll looking for mouse click
control and couldn't find anything. Is there a way for me to send a mouse
click too?
"Richie Hindle" <ri****@entrian.com> wrote in message
news:ma**********************************@python.o rg...

Is there a way I can control the mouse with python?

I assume you're on Windows. Here's how to move the mouse:
from ctypes import *
windll.user32.SetCursorPos(100, 100)

You can get ctypes from http://starship.python.net/crew/theller/ctypes/

As an added bonus, here's something that moves it relative to the
currently-focussed window, which is probably useful for what you want
(note that this one doesn't work on 95 or NT4 pre SP3, and it could use
some error handling).

from ctypes import *

user32 = windll.user32
kernel32 = windll.kernel32

class RECT(Structure):
_fields_ = [
("left", c_ulong),
("top", c_ulong),
("right", c_ulong),
("bottom", c_ulong)

class GUITHREADINFO(Structure):
_fields_ = [
("cbSize", c_ulong),
("flags", c_ulong),
("hwndActive", c_ulong),
("hwndFocus", c_ulong),
("hwndCapture", c_ulong),
("hwndMenuOwner", c_ulong),
("hwndMoveSize", c_ulong),
("hwndCaret", c_ulong),
("rcCaret", RECT)

def moveCursorInCurrentWindow(x, y):
# Find the focussed window.
guiThreadInfo = GUITHREADINFO(cbSize=sizeof(GUITHREADINFO))
user32.GetGUIThreadInfo(0, byref(guiThreadInfo))
focussedWindow = guiThreadInfo.hwndFocus

# Find the screen position of the window.
windowRect = RECT()
user32.GetWindowRect(focussedWindow, byref(windowRect))

# Finally, move the cursor relative to the window.
user32.SetCursorPos(windowRect.left + x, windowRect.top + y)

if __name__ == '__main__':
# Quick test.
moveCursorInCurrentWindow(100, 100)
Hope that helps,

Richie Hindle

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by simo | last post: by
5 posts views Thread by oglycans@yahoo.com | last post: by
2 posts views Thread by Anders Eriksson | last post: by
8 posts views Thread by Tom | last post: by
1 post views Thread by semagraw@gmail.com | last post: by
5 posts views Thread by EggHead | last post: by
2 posts views Thread by Synt4x | last post: by
3 posts views Thread by Lucas Prado Melo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.