14

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!

6 Answers 6

15

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

4 Comments

Do you know how to 'tap' the space key?
Take a look at these projs, space key must be mapped to ' '(string with space in it) github.com/Narengowda/web-mouse/blob/master/web_mouse/… github.com/SavinaRoja/PyUserInput/blob/master/pykeyboard/…
I came up with something like k.tab_key(k.keypad_keys['Space']) but it doesn't work. It is hard for me to understand the project documentation. Could you please help?
Did you try key_board.type_string(' ') ?
10

A more low-level approach would be to create an uinput device from which you would then inject input events into the linux input subsystem. Consider the following libraries:

Example of sending <enter> with the latter:

from evdev import uinput, ecodes as e

with uinput.UInput() as ui:
     ui.write(e.EV_KEY, e.KEY_ENTER, 1)
     ui.write(e.EV_KEY, e.KEY_ENTER, 0)
     ui.syn()

Comments

7

If the "model" is running graphically (with the X window system), the already-suggested xsendkey is a possibility, or xsendkeycode. If it's running textually (in a terminal window), then pexpect.

3 Comments

Hi! A corollary: While using pexpect, the following simple program doesn't seem to work for me: import pexpect child = pexpect.spawn('ls') fout = file('output.txt', 'w') child.logfile = fout I.e, output.txt is created but is empty when I cat it. pexpect is installed fine, since I can run the samples supplied with the program. Any suggestions would be appreciated! thanks!
The links to xsendkey and xsendkeycode are dead.
@hazzey here is a fork that works great: github.com/kyoto/sendkeys The xsendkey program is awesome to bypass restrictions when a simple copy-paste is not allowed and when you have to write tags followed by a validation with the enter / return key, like on Tumblr (a JavaScript hack would have been more complicated)
2

I recommend PyAutoGui. It's ridiculously simple to use, it's cross-platform and it's for Python 3 and 2.

In the linked page are listed the dependences and some code examples.

Comments

1

http://people.csail.mit.edu/adonovan/hacks/xsendkey.html

1 Comment

You can obviously take a look at the rather simple code of xsendkey and search for the corresponding calls in the Xlib binding for python.
0

As many of the solutions I have found in this and in another well ranked SO response were either deprecated (PyUserInput) or using evdev, which failed (UInputError: "/dev/uinput" cannot be opened for writing) the simplest solution for me using Linux was pynput. One example directly from their docs:

from pynput.keyboard import Key, Controller

keyboard = Controller()

# Press and release space
keyboard.press(Key.space)
keyboard.release(Key.space)

# Type a lower case A; this will work even if no key on the
# physical keyboard is labelled 'A'
keyboard.press('a')
keyboard.release('a')

# Type two upper case As
keyboard.press('A')
keyboard.release('A')
with keyboard.pressed(Key.shift):
    keyboard.press('a')
    keyboard.release('a')

# Type 'Hello World' using the shortcut type method
keyboard.type('Hello World')

It worked like a charm!

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.