mouse control with python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ken Favrow

    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?


  • Ken Favrow

    #2
    Re: mouse control with python

    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" <richie@entrian .com> wrote in message
    news:mailman.10 60685779.10849. [email protected] ...[color=blue]
    >
    > [Ken][color=green]
    > > Is there a way I can control the mouse with python?[/color]
    >
    > I assume you're on Windows. Here's how to move the mouse:
    >[color=green][color=darkred]
    > >>> from ctypes import *
    > >>> windll.user32.S etCursorPos(100 , 100)[/color][/color]
    >
    > 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
    > richie@entrian. com
    >
    >[/color]


    Comment

    Working...