LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 07-13-2012, 07:54 AM   #1
jtyrrell
LQ Newbie
 
Registered: Jul 2012
Posts: 1

Rep: Reputation: Disabled
Reading and writing to the Linux keyboard buffer


This is not a question, it is an answer post:

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.

#include <stdio.h>
#include <fcntl.h>
#include <linux/input.h>
#include <sstream>

#define EV_PRESSED 1
#define EV_RELEASED 0
#define EV_REPEAT 2

/*
* 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));

// Release the key
event.value = EV_RELEASED;
event.code = KEY_B;
write(fd, &event, sizeof(struct input_event));
close(fd);
}

// 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);
}
}

Last edited by jtyrrell; 07-13-2012 at 08:01 AM.
 
Old 07-13-2012, 09:12 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Thanks for posting your solution.
 
Old 10-08-2012, 11:53 AM   #3
twobob
LQ Newbie
 
Registered: Jul 2012
Posts: 3

Rep: Reputation: Disabled
Thumbs up X-compile for arm

Works okay on arm.

I cross-compiled this for arm
g++ -o keycodesKindle ./keycodes.cpp

required an extra
#include <unistd.h>
to kickstart.

Thanks for the reference
 
Old 10-21-2012, 06:58 PM   #4
alibenpeng
LQ Newbie
 
Registered: Oct 2012
Posts: 1

Rep: Reputation: Disabled
Exactly what I was looking for! Thanks!
 
Old 08-19-2016, 11:06 AM   #5
HerbertBall
LQ Newbie
 
Registered: Aug 2016
Posts: 1

Rep: Reputation: Disabled
Exclamation Problem on reading

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.

Someone has a clue?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
writing keyboard driver in linux archanac07 Linux - Software 7 01-29-2010 06:55 AM
Problem in reading/writing binary data in Linux esi-eric Linux - Hardware 3 07-20-2004 04:21 PM
reading and writing to Linux partitions from windows Moebius Linux - General 2 01-21-2004 04:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration