Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > mouse control with python

Reply
Thread Tools

mouse control with python

 
 
Ken Favrow
Guest
Posts: n/a
 
      08-12-2003
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?


 
Reply With Quote
 
 
 
 
Ken Favrow
Guest
Posts: n/a
 
      08-12-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> [Ken]
> > 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
> http://www.velocityreviews.com/forums/(E-Mail Removed)
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Screen Capture With Mouse , Mouse Position Capture Max Java 7 08-08-2009 11:51 PM
Menu Control - Expand items on mouse click rather than mouse hover =?Utf-8?B?am9qb2Jhcg==?= ASP .Net 1 10-02-2006 03:41 AM
Heres a mouse theres a mouse what a mouse do? unholy Gaming 37 09-17-2006 08:59 AM
tricky+frustrating: changing mouse handler while mouse pressed doesn't work Claus Atzenbeck Java 1 11-26-2004 09:40 AM
ASP.Net Datagrid get Cell Contents on mouse click or mouse down Davisro ASP .Net 2 04-23-2004 07:18 PM



Advertisments