Results 1 to 3 of 3
I want to record and play keyboard events on my Linux machine.
Basically i store all the keyboard events entered by the user into a text file. For this, i ...
- 05-14-2008 #1Just Joined!
- Join Date
- May 2008
- Location
- India
- Posts
- 15
Problems while writing to device file
I want to record and play keyboard events on my Linux machine.
Basically i store all the keyboard events entered by the user into a text file. For this, i am simply reading the device file: /dev/input/event0 using read() and storing the events into a text file. While playback i read() the text file and write() the events to the device file.
The code also stores and plays the time difference between the keys pressed.
The code works fine for mouse, but for keyboard it is giving some problems. While playback it shows the proper output including the delay but once the program terminates it dumps the entire entered string on the terminal.
Here's an example usage:
Recording
# ./record /dev/input/event0 ./record.txt
Now whatever the user enters thru the keyboard is recorded until the user presses the ctrl+c key.
Playback
# ./play /dev/input/event0 ./record.txt
Suppose i have entered "monty" while recording, and i have given 2 seconds delay after entering "mo" and then i entered "nty", here's the output:
mo(2seconds delay)nty
After this immediately the prompt returns and dumps the entire string:
#montyc
This "c" is shown as i had pressed Ctrl+C for stopping the recording...
Now, anybody knows while the entire string is getting dumped to the screen when the program terminates, and is there any way to resolve this.
Here's my code:
Recording
PlaybackCode:FILE *deviceConnection,*record_file; struct input_event myEvent; deviceConnection = fopen(argv[1], "rw"); record_file = fopen(argv[2], "rw+"); while (1) { printf("."); // read from the device file into the input_event structure */ fread(&myEvent, sizeof(struct input_event), 1, deviceConnection); // Write the events to the text file */ fwrite(&myEvent, sizeof(struct input_event), 1, record_file); fflush(record_file); } fclose(deviceConnection); fclose(record_file);
Code:FILE *record_file; struct input_event myEvent; char filename[128] ; int devHandle = -1; devHandle = open(argv[1], O_RDWR); record_file = fopen(argv[2], "r+"); while (!feof(record_file)) { fread(&myEvent, sizeof(struct input_event), 1, record_file); if (myEvent.value != 0) { printf ("We are in Key Press \n"); write(devHandle, &(myEvent), sizeof(struct input_event); } } close (devHandle); fclose(record_file);
- 05-14-2008 #2
A quick solution to your interest is simply to run "script" - which make typescript of terminal session to the file of your choice. Have a geek at "man script" and it will tell you all about it. If you wish to further extend "script" functionality, get the source code and do it. This is the essence of Linux.
G.
- 05-14-2008 #3Just Joined!
- Join Date
- May 2008
- Location
- India
- Posts
- 15
Hey, thanks for your reply!
Using the script command doesn't look good in my code. I would really like to use some other option.


Reply With Quote