Android原生C语言捕获按键

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/input.h>


static int event0_fd = -1;
struct input_event ev0[64];

//for handling event0, mouse/key/ts
static int handle_event0() {
int button = 0, realx = 0, realy = 0, i, rd;

rd = read(event0_fd, ev0, sizeof(struct input_event) * 64);
if ( rd < sizeof(struct input_event)) return 0;

for (i = 0; i < rd / sizeof(struct input_event); i++) {
printf("", ev0[i].type, ev0[i].code, ev0[i].value);
if (ev0[i].type == 3 && ev0[i].code == 0)
realx = ev0[i].value;
else if (ev0[i].type == 3 && ev0[i].code == 1)
realy = ev0[i].value;
else if (ev0[i].type == 1) {
if (ev0[i].code == 158) {
//if key esc then exit
return 0;
}
} else if (ev0[i].type == 0 && ev0[i].code == 0 && ev0[i].value == 0) {
realx = 0, realy = 0;
}
printf("event(%d): type: %d; code: %3d; value: %3d;\n", i,ev0[i].type, ev0[i].code, ev0[i].value);
}

return 1;
}

int main(void) {
int done = 1;
printf("sizeof(struct input_event) = %d\n", sizeof(struct input_event));

event0_fd = open("/dev/input/event1", O_RDWR);

if ( event0_fd < 0 )
return -1;

while ( done ) {
printf("begin handel_event0…\n");
done = handle_event0();
printf("end handel_event0…\n");
}

if ( event0_fd > 0 ) {
close(event0_fd);
event0_fd = -1;
}

return 0;

}

你可能感兴趣的:(android)