472,371 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

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
1 14799
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: simo | last post by:
I'm looking to make an app to record my mouse movements and play them back. It will essentially be for unit testing websites, so I suppose it could be done using one of the wxPython MSIE ActiveX...
by: oglycans | last post by:
Hi. Anybody know a way to control the mouse pointer (move it around and click on things) using python? Thanks.
by: red | last post by:
mouse events when the mouse is on a "child control" hi everyone; my problem: I have a userControl in this usercontrol, I have a child control (a button) when the mouse moves over the...
by: Anders Eriksson | last post by:
Hello! I have a program with three views ------------------------------------ | | | | 1 | 2 | | | ...
by: Tom | last post by:
Hi I am having problems working out if the mouse pointer is within the control bounds within the OnMouseMove method: Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs) ...
by: semagraw | last post by:
I found this link that describes the byte arrays to control the IM/Email Leds on my Logitech MX610 mouse: http://www.kdedevelopers.org/node/2029 The link to the tarball is dead so I can't look...
by: EggHead | last post by:
Hi all, My development environment is VB 6.0, and my OS is winxp. I would like to know how to find the mouse location at a control when the mouse is point at that control. The problem is that the...
by: Synt4x | last post by:
I'm creating a webcam user interface (to control google earth through my webcam) and I still can't find a good example to how to control the mouse to "click and drag" (not just click and release)....
by: Lucas Prado Melo | last post by:
I would like to control mouse events (i.e. I would like to "click" and move mouse around by code). How do I do this in python? regards
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.