[Tutor] Simulate Input from mouse and keyboard?

Alan Gauld alan.gauld at freenet.co.uk
Thu Mar 16 09:32:09 CET 2006

> Is there a way to control the mouse cursor's position so 
> I can tell it to move to X,Y position on the screen and 
> click or double click automatically?
>Also, how can you simulate input from the keyboard?

You can use the Windows API calls to do this, but its 
not easy and is extremely unreliable since even a small change 
of your screen layout will mess things up.

Check out the documentation for the PostMessage API call
on MSDN.

> I'd like to be able have a program that can call up a webpage, 
> click on a text box and type into it as though a person was 
> actually doing it.

But this is usually easier done in other ways by using a robotic 
browser instead opf trying to control the scren itself.

> I know this is kinda an obtuse way of getting things 
> done, but I think it'd be neat to be able to do it that way.

Interesting definition of neat. I presume you went to the 
same school as Heath-Robinson and others of that ilk?! :-)
Personally I call it the path of last resort, if not desparation!

> 1. How do you control the cursor relative to the entire screen, 
> not just locked inside the program's own window.

This requires PostMessage events using MouseMove events 
to position the mouse cursor.  But of course if anything moves 
your  real mouse - even by one pixel - it will screw things up.

> 2. How do you simulate mouse clicks/doubleclicks.
> 3. How do you emulate keyboard input as though a real person is typing.

These are both via PostMessage using button click and keypress events.

However I'd take a look at the urllib and browser modules plus 
the COM interface to InternetExplorer. All of these offer better
solutions albeit using different tricks to get there.

Alan G
Author of the learn to program web tutor

More information about the Tutor mailing list