How can I control the keyboard and mouse with Python?

Martin Trigaux picture Martin Trigaux · Dec 22, 2009 · Viewed 26.9k times · Source

How can I control the mouse and keyboard in Python?

The idea is to do the same as the Robot() class in Java. Be able to say: move the mouse from here to here, click there, write that whatever is on the screen.

For Windows there is win32api but I'm using mainly Linux.

For Linux there is Xlib but does it works for keyboard as well? (found only reference to the mouse)

Is there a cross-platform solution? (Linux, Windows and even OS X would be the great.)

Answer

Anurag Uniyal picture Anurag Uniyal · Dec 22, 2009

I use dogtail (https://fedorahosted.org/dogtail/) to do such things, using this I have created an automated testing framework for my Linux(Ubuntu) app. That framework clicks buttons and types into text fields.

see the gedit example, https://fedorahosted.org/dogtail/browser/examples/gedit-test-utf8-procedural-api.py

So just use dogtail e.g

dogtail.rawinput.click(100, 100)