Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
Do you want to simulate input at the kernel level (if so, on what OS), at the X level, in a terminal? –  Gilles Jun 12 '11 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. –  Naftuli Tzvi Kay Jun 12 '11 at 22:30
    
@TKKocheran: So, under AIX? FreeBSD? Linux? OSX? … The answer is highly likely to be completely different on each variant. –  Gilles Jun 12 '11 at 23:01
    
Sorry for not being specific, I'm operating under Linux, Ubuntu 11.04 64bit. –  Naftuli Tzvi Kay Jun 12 '11 at 23:23
    
What do you need that for? –  starblue Jun 13 '11 at 8:38

4 Answers 4

up vote 9 down vote accepted

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.

share|improve this answer
2  
I think xte operates at the same level. –  Jefromi Jun 13 '11 at 3:40
    
That works simply and quickly, thanks! –  Naftuli Tzvi Kay Jun 13 '11 at 5:48

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:

share|improve this answer
    
The link to einfochips.com was broken. I replaced it with a snapshot from archive.org. –  phunehehe Mar 25 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)? –  Aquarius Power Sep 25 at 0:07

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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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