[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.
Notices
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
or
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:
#include <linux/input.h>
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:
* /usr/src/linux-headers-3.2.0-23/include/linux
*
*/
main()
{
/************************************************
* IMPORTANT
* 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);
}
close(fd1);
}
}
I'm still having a problem. It looks like in the example there is not the line to read the input_event.
I suppose is something like:
read(fd1, &event, sizeof(struct input_event);
The problem is that the result is unexpected: the code read this way is always zero.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.