[Tutor] Mouse clicking

Alan Gauld alan.gauld at btinternet.com
Sat Jun 30 22:57:21 CEST 2007


"Lisa Barrott" <lisa.barrott at btinternet.com> wrote

> I was wondering if there was any way to force a mouse click at a set
> location using python even when the another window is focussed.
> I'm using Windows XP

Yes, you can use the Win32 API.
You need to get the target window handle with

-------------
HWND FindWindow(
    LPCTSTR lpClassName, // pointer to class name
    LPCTSTR lpWindowName  // pointer to window name
   );
 ---------------

or

--------------------
BOOL EnumWindows(
    WNDENUMPROC lpEnumFunc, // pointer to callback function
    LPARAM lParam  // application-defined value
   );
----------------------

Then you can use PostMessage() to generate a mouse event.

BOOL PostMessage(
    HWND hWnd, // handle of destination window
    UINT Msg, // message to post
    WPARAM wParam, // first message parameter
    LPARAM lParam  // second message parameter
   );

You can get the full docs on these functions on MSDN and the winall
package should provide access to them, or the ctypes module will
allow direct access for a bit more work.

There might be other more pythonic modules around that do the
heavy lifting for you, but these are not too hard to use.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list