Simulating Key Press event using Python for Linux

user308827 picture user308827 · Apr 4, 2010 · Viewed 40.9k times · Source

I am writing a script to automate running a particular model. When the model fails, it waits for a user input (Enter key). I can detect when the model has failed, but I am not able to use python (on linux) to simulate a key press event. Windows has the SendKeys library to do this but I was wondering if there is a similar library for python on linux.

Thanks!

Answer

naren picture naren · Oct 18, 2013

Have a look at this https://github.com/SavinaRoja/PyUserInput its cross-platform control for mouse and keyboard in python

Keyboard control works on X11(linux) and Windows systems. But no mac support(when i wrote this answer).

from pykeyboard import PyKeyboard
k = PyKeyboard()

# To Create an Alt+Tab combo
k.press_key(k.alt_key)
k.tap_key(k.tab_key)
k.release_key(k.alt_key)