Which is the easiest way to simulate keyboard and mouse on Python?

Question:

I need to do some macros and I wanna know what is the most recommended way to do it.

So, I need to write somethings and click some places with it and I need to emulate the TAB key to.

Asked By: Bruno 'Shady'

||

Answers:

Maybe you are looking for Sendkeys?

SendKeys is a Python module for
Windows that can send one or more
keystrokes or keystroke combinations
to the active window.

it seems it is windows only

Also you have pywinauto (copied from my SO answer)

pywinauto is a set of open-source
(LGPL) modules for using Python as a
GUI automation ‘driver’ for Windows NT
based Operating Systems (NT/W2K/XP).

and example from the web page

> from pywinauto import application
> app = application.Application.start("notepad.exe")
> app.notepad.TypeKeys("%FX")
> app.Notepad.MenuSelect("File->SaveAs")
> app.SaveAs.ComboBox5.Select("UTF-8")
> app.SaveAs.edit1.SetText("Example-utf8.txt")
> app.SaveAs.Save.Click()
Answered By: joaquin

I do automated testing stuff in Python. I tend to use the following:

http://www.tizmoi.net/watsup/intro.html
Edit: Link is dead, archived version: https://web.archive.org/web/20100224025508/http://www.tizmoi.net/watsup/intro.html

http://www.mayukhbose.com/python/IEC/index.php

I do not always (almost never) simulate key presses and mouse movement. I usually use COM to set values of windows objects and call their .click() methods.

You can send keypress signals with this:

import win32com.client

shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys("^a") # CTRL+A may "select all" depending on which window's focused
shell.SendKeys("{DELETE}") # Delete selected text?  Depends on context. :P
shell.SendKeys("{TAB}") #Press tab... to change focus or whatever

This is all in Windows. If you’re in another environment, I have no clue.

Answered By: Ishpeck

pyautogui is a great package to send keys and automate several keyboard / mouse related tasks. Check out Controlling the Keyboard and Mouse with GUI Automation and PyAutoGUI’s documentation.

Answered By: Mitesh Budhabhatti

You can use PyAutoGUI library for Python which works on Windows, macOS, and Linux.

Mouse

Here is a simple code to move the mouse to the middle of the screen:

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

Docs page: Mouse Control Functions.

Related question: Controlling mouse with Python.

Keyboard

Example:

pyautogui.typewrite('Hello world!')                 # prints out "Hello world!" instantly
pyautogui.typewrite('Hello world!', interval=0.25)  # prints out "Hello world!" with a quarter second delay after each character

Docs page: Keyboard Control Functions.


More reading: Controlling the Keyboard and Mouse with GUI Automation (Chapter 18 of e-book).

Related questions:

Answered By: kenorb

Two other options are:

Warning – if you are wanting to use keyboard control for games, then pynput doesn’t always work – e.g. it works for Valheim, but not for the Witcher 3 – which is where PyDirectInput will work instead. I also tested PyDirectInput and it works for Half life 2 (as a test of an older game).

Tip – You will likely need to reduce (don’t remove for games) the delay between character typing – use pydirectinput.PAUSE = 0.05

As an example, here is a function that allows virtual keyboard typing – currently only tested on Windows:

from pynput import keyboard
try:
    import pydirectinput
    pydirectinput.PAUSE = 0.05
except ImportError as err:
    pydirectinput = False
    print("pydirectinput not found:")

def write_char(ch):
    upper = ch.isupper()
    if pydirectinput and pydirectinput.KEYBOARD_MAPPING.get(ch.lower(), False):
        if upper:
            pydirectinput.keyDown('shift')
            print('^')
        pydirectinput.write(ch.lower(), interval=0.0)
        print(ch)
        if upper:
            pydirectinput.keyUp('shift')
    else:
        keyboard.Controller().type(ch)

This allows a string to be sent in, with upper case alphabetic characters handled through pydirectinput. When characters don’t simply map, the function falls back to using pynput. Note that PyAutoGUI also can’t handled some shifted characters – such as the £ symbol, etc.

Answered By: AndyS
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.