解析从键盘接收到的字节

解析从键盘接收到的字节

我目前正在努力了解计算机或操作系统(这里是 Linux)接收的键盘输入。

我编写了一个小程序来查看来自键盘的所有输入,我所能理解的是每条消息长 24 个字节,并且每次按键似乎都记录在 3 条这样的消息中。

我无法理解该条目的确切含义。

举个例子:

  1. 我的参赛作品
helloworld^C
  1. 输出

    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. 我使用的代码
#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

我无法理解该条目的确切含义。

你的程序正在读取的设备节点,

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

是一种特殊类型的文件,称为输入事件接口
读取该文件将返回以下组织的数据

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

除了键/事件code和键之外value,数据还包括时间戳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.

描述了事件类型和事件代码这里

尝试使用该实用程序事件测试而不是你的程序来感受事件接口。
例如按下和释放键的示例输出A

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

确保您了解原始键盘输出,即扫描码。
在 PC 上,键盘硬件会生成扫描码,以便可以检测到每次按键(和释放)。
软件负责翻译和重新映射按键,例如转换为 ASCII 码或ANSI 转义序列

相关内容