473,837 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15170
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******** *************** ***********@pyt hon.org...

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.S etCursorPos(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(S tructure):
_fields_ = [
("cbSize", c_ulong),
("flags", c_ulong),
("hwndActive ", c_ulong),
("hwndFocus" , c_ulong),
("hwndCaptur e", c_ulong),
("hwndMenuOwner ", c_ulong),
("hwndMoveSize" , c_ulong),
("hwndCaret" , c_ulong),
("rcCaret", RECT)

def moveCursorInCur rentWindow(x, y):
# Find the focussed window.
guiThreadInfo = GUITHREADINFO(c bSize=sizeof(GU ITHREADINFO))
user32.GetGUITh readInfo(0, byref(guiThread Info))
focussedWindow = guiThreadInfo.h wndFocus

# Find the screen position of the window.
windowRect = RECT()
user32.GetWindo wRect(focussedW indow, byref(windowRec t))

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

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

Richie Hindle
ri****@entrian. com

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 widgets, but I'd like it to work on other platforms and not just in web browsers. Anyone got any ideas for how this could be done? I guess if reading/writing mouse movements is difficult, I could limit
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 userControl, I can detect the movement using MouseMove event. the problem is when the mouse is on the child control : I can t detect
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) Debug.WriteLine(Me.Bounds.Contains(e.X, e.Y)) End Sub
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 at that. Is there anyway to do what the link says in Python? If you follow the link above, it talks about 'sending the byte array to 0x10 (16)'. I'm not sure if that is possible in python. Can anyone help or point me in the right direction? ...
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 mouse "whatever" events will not fire since the mouse is hjhack by other control. Anyway, I have the following code and I can check is the mouse over the control or not, however, I cannot find out where the mouse location at that control. Private...
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). I want to move an object in front of the webcam and move maps on google earth. I hope someone understands what i need.
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: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.