tags:

views:

606

answers:

5

Hello,

How can I control the mouse and the 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, clic 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 OsX would be the great)?

thank you

+1  A: 

Here is an interessting Thread from Python Forum for you: Python Forum

Edit: There was also an interessting question on stackoverflow regarding mouse control...maybe it is a good starting point.. Mouse Control with Python

One of the Answers is refering to an Linux example...which heads you to an nice blog entry.

bastianneu
funny, I just send the same link to Dominic Rodger. It works on windows only I think (windll.user32.SetCursorPos doesn't sound good)
Martin Trigaux
A: 

for the mouse, I've found pymouse which seems to work (I haven't fully tried it, a small hack needed for the click, cf the issues)

for the keyboard, I'm not sure Xlib can do the job. I'm still looking on how to write something but you can catch key event as explained here or in C here using Xlib (but I don't know C).

here is an example working on gnome only (not good enough yet)

In pymouse, they have a nice way to make it work on the 3 different platform but needs to make 3 code...

Martin Trigaux
+1  A: 

For console try ncurses or slang. In other situation try PyQt, PyGtk, TkInter.

ALL of this solution ARE cross-platform and work almost anywhere.

vitaly.v.ch
it's already sad that I can't find a solution for windows and linux so a different solution if you are using Gtk or Qt seems not good. and anyway I don't understand what you say to use. Which method ? how ?...
Martin Trigaux
Why Gtk or Qt seems for You not good? TkInter as far as I know is embedded in python by default!!!
vitaly.v.ch
Hm. May be You want emulating user action?
vitaly.v.ch
yes that's basically what I want to do. I want to be able to say go from here to here, clic there and write that whatever there is on the screen. Can I do it with PyQtk, PyGtk or PkInter ?
Martin Trigaux
It MUST be available via PyGtk. about other toolkit i'm not sure.
vitaly.v.ch
found this thread : http://stackoverflow.com/questions/860013/a-python-equivilent-to-java-robot which also advice to use gtk. But I still don't see how to move the mouse or send keyboard event. I see only function to catch event
Martin Trigaux
http://www.pygtk.org/docs/pygtk/class-gdkdisplay.html#method-gdkdisplay--put-event
vitaly.v.ch
+2  A: 

I use dogtail (https://fedorahosted.org/dogtail/) to do such things, using this I have created a automated testing fromework 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)
Anurag Uniyal
great, the rawinput module seems to have everything I need thanks a lot. I read that for the function to really test apps (detecting menus,...) work only for gnome.Does the rawinput module work for other GUI ? (anyway it's open source, I can have a look)
Martin Trigaux
do you know why if I do a dogtail.rawinput.click(x, y) I have to do a dogtail.rawinput.pressKey('a') otherwise nothing happens ???
Martin Trigaux
wierd it works on ubuntu without any pressKey
Anurag Uniyal
I've send a bug report. can you try the piece of code ? https://bugzilla.gnome.org/show_bug.cgi?id=605302
Martin Trigaux
it works for me on Ubuntu 9.10 32bit, not on Debian Sid amd64 and Archlinux amd64. A bug from the 64 bit version ?
Martin Trigaux
+1  A: 

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

Yes, it work for keyboard also.

vitaly.v.ch
great and I think that Xlib is more generic than gtk (cf xlib's page on wikipedia) but same as for pygtk, I don't see the function to do it...
Martin Trigaux
ok found with Xlib : pymouse for the mouse and here (http://www.tuxisalive.com/tux-droid-forum/tux-gadgets/245877021) for the keyboard
Martin Trigaux