[SOLVED] Reading and writing to the Linux keyboard buffer
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I had a project where I needed to stuff the Linux keyboard with some keystrokes to control a web browser remotely. I couldn't believe how little information there was on the keyboard buffer and how much time it took me to figure it all out. I saw many posts with similar questions but very few with any real working answers and code. So, I thought I'd post what I figured out.
1) First determine the event ID of your keyboard at the command line using either: $cat /proc/bus/input/devices
viewing the var/log/Xorg.0.log searching for "keyboard"
2) Edit the posted code and insert the event id for your system in the following code segment:
char *device = "/dev/input/event4";
3) When running the program you will need to be logged in a su or sudo user.
Run the command with: $ sudo ./a.out
4) All keyboard key defines can be located in the header file:
Here is a snippet of a working writer/reader in its entirety:
I hope this can help others.
* Purpose: Stuffs the Linux keyboard buffer with a key and
* reads it back out of the buffer.
* All key definitions can be found in input.h file:
* you need to execute this code as the su or
* sudo user in order to open the device properly.
printf("Starting the keyboard buffer writer/reader \n");
int fd = 0;
char *device = "/dev/input/event4"; // This is the keyboard device as identified using both: $cat /proc/bus/input/devices
// and looking in the var/log/Xorg.0.log searching for "keyboard"
// Write a key to the keyboard buffer
if( (fd = open(device, O_RDWR)) > 0 )
struct input_event event;
printf("The keyboard code is: %d \n", KEY_B); // Note: these are not the same as ASCII codes.
// Press a key (stuff the keyboard with a keypress)
event.type = EV_KEY;
event.value = EV_PRESSED;
event.code = KEY_B;
write(fd, &event, sizeof(struct input_event));
// Read the key back from the keyboard buffer
int fd1 = 0;
if( (fd1 = open(device, O_RDONLY)) > 0 ) // It's important to open a new file descriptor here or the program will block.
struct input_event event;
unsigned int scan_code = 0;
if(event.type != EV_KEY)
return 0; // Keyboard events are always of type EV_KEY
if(event.value == EV_RELEASED)
scan_code = event.code;
printf("read back scan_code is: %d \n", scan_code);