Atualmente estou trabalhando para entender as entradas do teclado recebidas pelo computador ou sistema operacional, que aqui é o Linux.
Eu escrevi um pequeno programa para ver todas as entradas do teclado, e tudo que consegui entender foi que cada mensagem tem 24 bytes e cada pressionamento de tecla parece ser gravado em 3 dessas mensagens.
Não consegui encontrar uma maneira de entender exatamente o que a entrada significava.
Como um exemplo:
- Minha entrada
helloworld^C
- A saída
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
- O código que usei
#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++;
}
}
Responder1
Não consegui encontrar uma maneira de entender exatamente o que a entrada significava.
O nó do dispositivo do qual seu programa está lendo,
FILE* fp = fopen("/dev/input/event8","r");
é um tipo especial de arquivo conhecido comointerface de evento de entrada.
A leitura desse arquivo retornará dados organizados como
struct input_event {
struct timeval time;
unsigned short type;
unsigned short code;
unsigned int value;
};
Além de key/event code
e key value
, os dados incluem um 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.
Os tipos de eventos e códigos de eventos são descritosaqui.
Tente usar o utilitárioteste de eventoem vez do seu programa para ter uma ideia da interface do evento.
Exemplo de saída do EG para pressionar e soltar a A
tecla:
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
Certifique-se de entender a saída bruta do teclado, ou seja, scancodes.
Em um PC, o hardware do teclado gera scancodes para que cada pressionamento de tecla (e liberação) possa ser detectado.
Cabe ao software traduzir e remapear as teclas pressionadas, por exemplo, para código ASCII ouSequências de escape ANSI.