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

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Martin Trigaux
  • 5,311
  • 9
  • 45
  • 58
  • 1
    Python's powerful, but I don't think it's powerful enough to allow you to control a keyboard and mouse. I'd love it if you could actually make my mouse zoom across my desk using Python, but I don't think it's going to happen. ;-) – Dominic Rodger Dec 22 '09 at 12:53
  • a good solution for windows only http://www.python-forum.org/pythonforum/viewtopic.php?f=2&t=8976 I'm looking for the same for linux or cross-platform – Martin Trigaux Dec 22 '09 at 13:00
  • Hm. May be You want emulating user action? – vitaly.v.ch Dec 23 '09 at 15:44
  • possible duplicate of [Is there a Python equivalent to Java's AWT Robot class?](http://stackoverflow.com/questions/860013/is-there-a-python-equivalent-to-javas-awt-robot-class) – Oded Nov 02 '11 at 20:01
  • Related: [Controlling mouse with Python](https://stackoverflow.com/q/1181464/55075). – kenorb Jan 07 '18 at 13:06
  • 2
    Possible duplicate of [Which is the easiest way to simulate keyboard and mouse on Python?](https://stackoverflow.com/questions/2791839/which-is-the-easiest-way-to-simulate-keyboard-and-mouse-on-python) – kenorb Jan 07 '18 at 13:09

8 Answers8

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)
Vineeth Sai
  • 3,389
  • 7
  • 23
  • 34
Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
  • 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 Dec 23 '09 at 09:26
  • 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 Dec 23 '09 at 11:41
  • I've send a bug report. can you try the piece of code ? https://bugzilla.gnome.org/show_bug.cgi?id=605302 – Martin Trigaux Dec 23 '09 at 13:52
  • 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 Dec 23 '09 at 14:44
  • is there anything alike for windows? – Manticore Sep 01 '16 at 12:09
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.

kenorb
  • 155,785
  • 88
  • 678
  • 743
serfer2
  • 2,573
  • 1
  • 23
  • 17
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
Alex Gray
  • 16,007
  • 9
  • 96
  • 118
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.

Community
  • 1
  • 1
bastianneu
  • 2,059
  • 2
  • 18
  • 31
  • 1
    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 Dec 22 '09 at 13:02
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...

Martin Trigaux
  • 5,311
  • 9
  • 45
  • 58
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.

vitaly.v.ch
  • 2,485
  • 4
  • 26
  • 36
  • 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 Dec 23 '09 at 09:09
  • Why Gtk or Qt seems for You not good? TkInter as far as I know is embedded in python by default!!! – vitaly.v.ch Dec 23 '09 at 15:39
  • Hm. May be You want emulating user action? – vitaly.v.ch Dec 23 '09 at 15:42
  • 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 Dec 23 '09 at 17:24
  • It MUST be available via PyGtk. about other toolkit i'm not sure. – vitaly.v.ch Dec 23 '09 at 17:59
  • 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 Dec 23 '09 at 18:17
  • http://www.pygtk.org/docs/pygtk/class-gdkdisplay.html#method-gdkdisplay--put-event – vitaly.v.ch Dec 24 '09 at 08:08
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.

vitaly.v.ch
  • 2,485
  • 4
  • 26
  • 36
  • 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 Dec 23 '09 at 21:14
  • 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 Dec 23 '09 at 21:37
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.

Eloims
  • 5,106
  • 4
  • 25
  • 41