27

I'm trying to inject keystrokes into the input daemon so as to simulate typing from a Bash script. Is this possible, and if so, how can I make it happen?

6
  • Do you want to simulate input at the kernel level (if so, on what OS), at the X level, in a terminal? Jun 12, 2011 at 22:26
  • At the kernel level, I'd like it to essentially appear as if I hit an actual key on an actual keyboard. Jun 12, 2011 at 22:30
  • @TKKocheran: So, under AIX? FreeBSD? Linux? OSX? … The answer is highly likely to be completely different on each variant. Jun 12, 2011 at 23:01
  • 1
    What do you need that for?
    – starblue
    Jun 13, 2011 at 8:38
  • 3
    Perhaps I'd like to write a script that randomly inserts keypresses at random intervals. Jun 13, 2011 at 17:50

4 Answers 4

24

Use the uinput driver. I don't think there's a utility for that; you're going to have to write or adapt a bit of C code. In a nutshell:

#include <fcntl.h>
#include <ioctl.h>
#include <unistd.h>
#include <linux/input.h>
#include <linux/uinput.h>
/* Set up a fake keyboard device */
fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK); // or /dev/input/uinput
ioctl(fd, UI_SET_EVBIT, EV_KEY);
struct uinput_user_dev uidev = …;
write(fd, &uidev, sizeof(uidev));
ioctl(fd, UI_DEV_CREATE);
/* Send an event */
struct input_event ev = …;
write(fd, &ev, sizeof(ev));
/* Clean up */
ioctl(fd, UI_DEV_DESTROY);
close(fd);

More complete references:

5
  • The link to einfochips.com was broken. I replaced it with a snapshot from archive.org.
    – phunehehe
    Mar 25, 2014 at 6:21
  • do you think this could help on solving this question? unix.stackexchange.com/questions/156985/…, I thought, when I press a key, this program would send another key that the problematic application actually recognizes (even if at the same time)? Sep 25, 2014 at 0:07
  • Thiemonge's sample code for creating and moving around a mouse works, can't figure out how to make keystrokes from it though even if it supposedly teaches that.
    – Cestarian
    Mar 1, 2016 at 18:13
  • @Cestarian This sounds like a question for Stack Overflow. Post your code, explain what you expected and what you see instead, and provide any relevant error messages, log entries etc. Mar 1, 2016 at 18:38
  • @Gilles well I'd love to, but it says i've reached my question limit... For getting too many downvotes on 2 questions I asked a while back; and probably some more on much older questions I deleted. Hell, I don't even know why I got so many downvotes here stackoverflow.com/questions/20619023/… (got a pretty good answer though), was it because I didn't post the error I was receiving maybe? Anyhow, off topic. Basically I would but can't. :(
    – Cestarian
    Mar 1, 2016 at 19:20
23

If you are operating at the X level (as in Gilles' question), then use xdotool like so:

xdotool key KEYSTROKE_SPECIFIER

Where KEYSTROKE_SPECIFIER can be something like "a" or "F2" or "control+j"

EDIT: I missed your response to Gilles' question, sorry. I'll leave this response here as a solution for the X-case.

1
  • 3
    I think xte operates at the same level.
    – Cascabel
    Jun 13, 2011 at 3:40
11

If you're NOT working with X programs with windows that can be sent keys, you are probably looking for expect, a handy and very configurable program for running other interactive shell programs as if a user were controlling the terminal. You can setup programatic responses to respond to various output with different inputs.

7

I wrote some Python code that does that. You can find it in my open source project.

http://code.google.com/p/pycopia/source/browse/trunk/core/pycopia/OS/Linux/event.py

If you run that module as a script as root you can see a demo in action.

This basic functionality was extended for another project, powerdroid, that provides more concrete implementation for an embedded system (Android).

http://code.google.com/p/powerdroid/source/browse/trunk/src/droid/devices.py

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.