Find the answer to your Linux question:
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 ...
  1. #1
    Just Joined!
    Join Date
    May 2008
    Location
    India
    Posts
    15

    Question 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
    Code:
    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);
    Playback
    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);

  2. #2
    Just Joined! wildpossum's Avatar
    Join Date
    Apr 2008
    Location
    Sydney/Australia
    Posts
    92
    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.

  3. #3
    Just Joined!
    Join Date
    May 2008
    Location
    India
    Posts
    15

    Wink

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...