-1

I am currently working with understanding the keyboard entries received by the computer or OS, which here is Linux.

I wrote a small program to see all the entries from the keyboard, and all I could understand was that each message is 24 bytes long and each key press seems to be recorded within 3 such messages.

I could not find a way to understand what the entry meant exactly.

As an example:

  1. My Entry
helloworld^C
  1. The output

    ffffffd3 31 5d 5d 0 0 0 0 ffffff98 3d c 0 0 0 0 0 4 0 4 0 28 0 7 0 
    ffffffd3 31 5d 5d 0 0 0 0 ffffff98 3d c 0 0 0 0 0 1 0 1c 0 0 0 0 0 
    ffffffd3 31 5d 5d 0 0 0 0 ffffff98 3d c 0 0 0 0 0 0 0 0 0 0 0 0 0 
    ffffffd4 31 5d 5d 0 0 0 0 ffffffc2 5d 9 0 0 0 0 0 4 0 4 0 b 0 7 0 
    ffffffd4 31 5d 5d 0 0 0 0 ffffffc2 5d 9 0 0 0 0 0 1 0 23 0 1 0 0 0 
    ffffffd4 31 5d 5d 0 0 0 0 ffffffc2 5d 9 0 0 0 0 0 0 0 0 0 0 0 0 0 
    ffffffd4 31 5d 5d 0 0 0 0 2c 5b a 0 0 0 0 0 4 0 4 0 b 0 7 0 
    ffffffd4 31 5d 5d 0 0 0 0 2c 5b a 0 0 0 0 0 1 0 23 0 0 0 0 0 
    ffffffd4 31 5d 5d 0 0 0 0 2c 5b a 0 0 0 0 0 0 0 0 0 0 0 0 0 
    ffffffd4 31 5d 5d 0 0 0 0 ffffffc8 ffffffd9 a 0 0 0 0 0 4 0 4 0 8 0 7 0 
    ffffffd4 31 5d 5d 0 0 0 0 ffffffc8 ffffffd9 a 0 0 0 0 0 1 0 12 0 1 0 0 0 
    ffffffd4 31 5d 5d 0 0 0 0 ffffffc8 ffffffd9 a 0 0 0 0 0 0 0 0 0 0 0 0 0 
    ffffffd4 31 5d 5d 0 0 0 0 ffffffe7 16 c 0 0 0 0 0 4 0 4 0 8 0 7 0 
    ffffffd4 31 5d 5d 0 0 0 0 ffffffe7 16 c 0 0 0 0 0 1 0 12 0 0 0 0 0 
    ffffffd4 31 5d 5d 0 0 0 0 ffffffe7 16 c 0 0 0 0 0 0 0 0 0 0 0 0 0 
    ffffffd4 31 5d 5d 0 0 0 0 ffffffac 11 e 0 0 0 0 0 4 0 4 0 f 0 7 0 
    ffffffd4 31 5d 5d 0 0 0 0 ffffffac 11 e 0 0 0 0 0 1 0 26 0 1 0 0 0 
    ffffffd4 31 5d 5d 0 0 0 0 ffffffac 11 e 0 0 0 0 0 0 0 0 0 0 0 0 0 
    ffffffd4 31 5d 5d 0 0 0 0 ffffffc9 ffffffcf e 0 0 0 0 0 4 0 4 0 f 0 7 0 
    ffffffd4 31 5d 5d 0 0 0 0 ffffffc9 ffffffcf e 0 0 0 0 0 1 0 26 0 0 0 0 0 
    ffffffd4 31 5d 5d 0 0 0 0 ffffffc9 ffffffcf e 0 0 0 0 0 0 0 0 0 0 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 4c ffffffca 0 0 0 0 0 0 4 0 4 0 f 0 7 0 
    ffffffd5 31 5d 5d 0 0 0 0 4c ffffffca 0 0 0 0 0 0 1 0 26 0 1 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 4c ffffffca 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 4d ffffffc7 1 0 0 0 0 0 4 0 4 0 f 0 7 0 
    ffffffd5 31 5d 5d 0 0 0 0 4d ffffffc7 1 0 0 0 0 0 1 0 26 0 0 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 4d ffffffc7 1 0 0 0 0 0 0 0 0 0 0 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 ffffffab 0 4 0 0 0 0 0 4 0 4 0 12 0 7 0 
    ffffffd5 31 5d 5d 0 0 0 0 ffffffab 0 4 0 0 0 0 0 1 0 18 0 1 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 ffffffab 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 ffffffb4 fffffffd 4 0 0 0 0 0 4 0 4 0 12 0 7 0 
    ffffffd5 31 5d 5d 0 0 0 0 ffffffb4 fffffffd 4 0 0 0 0 0 1 0 18 0 0 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 ffffffb4 fffffffd 4 0 0 0 0 0 0 0 0 0 0 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 ffffffbc ffffffbb 5 0 0 0 0 0 4 0 4 0 1a 0 7 0 
    ffffffd5 31 5d 5d 0 0 0 0 ffffffbc ffffffbb 5 0 0 0 0 0 1 0 11 0 1 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 ffffffbc ffffffbb 5 0 0 0 0 0 0 0 0 0 0 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 ffffffc2 ffffffb8 6 0 0 0 0 0 4 0 4 0 1a 0 7 0 
    ffffffd5 31 5d 5d 0 0 0 0 ffffffc2 ffffffb8 6 0 0 0 0 0 1 0 11 0 0 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 ffffffc2 ffffffb8 6 0 0 0 0 0 0 0 0 0 0 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 ffffffa8 ffffffb5 7 0 0 0 0 0 4 0 4 0 12 0 7 0 
    ffffffd5 31 5d 5d 0 0 0 0 ffffffa8 ffffffb5 7 0 0 0 0 0 1 0 18 0 1 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 ffffffa8 ffffffb5 7 0 0 0 0 0 0 0 0 0 0 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 ffffffa3 73 8 0 0 0 0 0 4 0 4 0 12 0 7 0 
    ffffffd5 31 5d 5d 0 0 0 0 ffffffa3 73 8 0 0 0 0 0 1 0 18 0 0 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 ffffffa3 73 8 0 0 0 0 0 0 0 0 0 0 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 79 70 9 0 0 0 0 0 4 0 4 0 15 0 7 0 
    ffffffd5 31 5d 5d 0 0 0 0 79 70 9 0 0 0 0 0 1 0 13 0 1 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 79 70 9 0 0 0 0 0 0 0 0 0 0 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 fffffff8 6d a 0 0 0 0 0 4 0 4 0 15 0 7 0 
    ffffffd5 31 5d 5d 0 0 0 0 fffffff8 6d a 0 0 0 0 0 1 0 13 0 0 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 fffffff8 6d a 0 0 0 0 0 0 0 0 0 0 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 c ffffffec a 0 0 0 0 0 4 0 4 0 f 0 7 0 
    ffffffd5 31 5d 5d 0 0 0 0 c ffffffec a 0 0 0 0 0 1 0 26 0 1 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 c ffffffec a 0 0 0 0 0 0 0 0 0 0 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 fffffffa ffffffa9 b 0 0 0 0 0 4 0 4 0 f 0 7 0 
    ffffffd5 31 5d 5d 0 0 0 0 fffffffa ffffffa9 b 0 0 0 0 0 1 0 26 0 0 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 fffffffa ffffffa9 b 0 0 0 0 0 0 0 0 0 0 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 17 ffffffa7 c 0 0 0 0 0 4 0 4 0 7 0 7 0 
    ffffffd5 31 5d 5d 0 0 0 0 17 ffffffa7 c 0 0 0 0 0 1 0 20 0 1 0 0 0 
    ffffffd5 31 5d 5d 0 0 0 0 17 f
  1. The code I used
#include"json.h" 

    int main(char* argc, char* argv[]){
        FILE* fp = fopen("/dev/input/event8","r");
        FILE* fp2 = fopen("log.txt","w");
        char ch;
        int n = 0;
        char repr[10];
        while(1){
            ch = fgetc(fp);
            if(n==24){
                fputc('\n', fp2);
                n = 0;
            }
            sprintf(repr, "%x", ch);
            fputs(repr, fp2);
            fputc(' ', fp2);
            n++;
        }
    }

1 Answers1

0

I could not find a way to understand what the entry meant exactly.

The device node that your program is reading from,

        FILE* fp = fopen("/dev/input/event8","r");

is a special type of file that is known as an input event interface.
Reading from that file will return data organized as

struct input_event {
        struct timeval time;
        unsigned short type;
        unsigned short code;
        unsigned int value;
};

Besides the key/event code and key value, the data includes a timestamp time.

The GNU C Library provides two data types specifically for representing an elapsed time. 
They are used by various GNU C Library functions, and you can use them for your own purposes 
too. They’re exactly the same except that one has a resolution in microseconds, and the other, 
newer one, is in nanoseconds.

Data Type: struct timeval

    The struct timeval structure represents an elapsed time. It is declared in sys/time.h 
    and has the following members:

    time_t tv_sec
        This represents the number of whole seconds of elapsed time.

    long int tv_usec
        This is the rest of the elapsed time (a fraction of a second), represented as 
        the number of microseconds. It is always less than one million.

The event types and event codes are described here.

Try using the utility evtest instead of your program to get a feel for the event interface.
E.G. sample output for pressing & releasing the A key:

Event: time 1566419656.576267, -------------- SYN_REPORT ------------
Event: time 1566419662.463531, type 4 (EV_MSC), code 4 (MSC_SCAN), value 1e
Event: time 1566419662.463531, type 1 (EV_KEY), code 30 (KEY_A), value 1
Event: time 1566419662.463531, -------------- SYN_REPORT ------------
Event: time 1566419662.615625, type 4 (EV_MSC), code 4 (MSC_SCAN), value 1e
Event: time 1566419662.615625, type 1 (EV_KEY), code 30 (KEY_A), value 0

Be sure that you understand raw keyboard output, i.e. scancodes.
On a PC the keyboard hardware generates scancodes so that every keypress (and release) can be detected.
It is up to software to translate and remap the keypresses, e.g. to ASCII code or ANSI escape sequences.

sawdust
  • 18,591