mouse control with python

Discussion in 'Python' started by Ken Favrow, Aug 12, 2003.

  1. Ken Favrow

    Ken Favrow Guest

    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, Aug 12, 2003
    #1
    1. Advertising

  2. Ken Favrow

    Ken Favrow Guest

    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" <> wrote in message
    news:...
    >
    > [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
    >
    >
    >
    Ken Favrow, Aug 12, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Davisro
    Replies:
    2
    Views:
    1,285
    Jim Corey
    Apr 23, 2004
  2. Max
    Replies:
    7
    Views:
    9,076
  3. Claus Atzenbeck
    Replies:
    1
    Views:
    1,060
    Claus Atzenbeck
    Nov 26, 2004
  4. =?Utf-8?B?am9qb2Jhcg==?=
    Replies:
    1
    Views:
    4,066
    wwwtar
    Oct 2, 2006
  5. tom arnall
    Replies:
    6
    Views:
    1,255
    Ian Shef
    Jan 18, 2007
Loading...

Share This Page