Simulate keystroke in Linux with Python

microo8 picture microo8 · Apr 19, 2011 · Viewed 45.9k times · Source

How can I simulate a keystroke in python? I also want to press multiple keys simultaneously.

Something like:

keystroke('CTRL+F4')

or

keystroke('Shift+A')

Answer

gvalkov picture gvalkov · Aug 23, 2012

Consider python-uinput and evdev. Example of shift+a with the latter:

from evdev import uinput, ecodes as e

with uinput.UInput() as ui:
    ui.write(e.EV_KEY, e.KEY_LEFTSHIFT, 1)
    ui.write(e.EV_KEY, e.KEY_A, 1)
    ui.syn()