12

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.)

6

8 Answers 8

6

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)
Sign up to request clarification or add additional context in comments.

5 Comments

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)
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 ???
I've send a bug report. can you try the piece of code ? bugzilla.gnome.org/show_bug.cgi?id=605302
it works for me on Ubuntu 9.10 32bit, not on Debian Sid amd64 and Archlinux amd64. A bug from the 64 bit version ?
is there anything alike for windows?
3

I can advise you PyAutoGUI, it allows to full control Mouse and Keyboard and get Screenshots and even you can locate images within the screen (like: where is the button?), very useful to automate clicks dynamically. It works for Windows, macOS, and Linux.

For example:

>>> import pyautogui
>>> screenWidth, screenHeight = pyautogui.size()
>>> pyautogui.moveTo(screenWidth / 2, screenHeight / 2)

Check out the Introduction page.

Comments

2

This totally works... on a Mac at least. This is for a click AND drag, etc.. but can be retrofitted accordingly.

#!/usr/bin/python
import sys
import time
from Quartz.CoreGraphics import * # imports all of the top-level symbols in the module

def mouseEvent(type, posx, posy):
    theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
    CGEventPost(kCGHIDEventTap, theEvent)
def mousemove(posx,posy):
    mouseEvent(kCGEventMouseMoved, posx,posy);
def mouseclickdn(posx,posy):
    mouseEvent(kCGEventLeftMouseDown, posx,posy);
def mouseclickup(posx,posy):
    mouseEvent(kCGEventLeftMouseUp, posx,posy);
def mousedrag(posx,posy):
    mouseEvent(kCGEventLeftMouseDragged, posx,posy);

ourEvent = CGEventCreate(None);
currentpos=CGEventGetLocation(ourEvent); # Save current mouse position
mouseclickdn(60, 100);
mousedrag(60, 300);
mouseclickup(60, 300);
time.sleep(1);
mousemove(int(currentpos.x),int(currentpos.y)); # Restore mouse position

Comments

1

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.

1 Comment

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

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...

Comments

1

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.

2 Comments

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...
ok found with Xlib : pymouse for the mouse and here (tuxisalive.com/tux-droid-forum/tux-gadgets/245877021) for the keyboard
1

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

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

7 Comments

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 ?...
Why Gtk or Qt seems for You not good? TkInter as far as I know is embedded in python by default!!!
Hm. May be You want emulating user action?
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 ?
It MUST be available via PyGtk. about other toolkit i'm not sure.
|
1

A cross platform solution on linux, windows and mac is autopy. https://github.com/msanders/autopy/

It allows controlling mouse and keyboard, taking screenshots, and finding small bitmaps on larger bitmaps and should be very convenient if you want to automate some gui application you have no control on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.