Android原生C语言捕获按键

#include<stdio.h>

#include<stdlib.h>

#include<fcntl.h>

#include<linux/input.h>

staticintevent0_fd=-1;

structinput_eventev0[64];

//forhandlingevent0,mouse/key/ts

staticinthandle_event0(){

intbutton=0,realx=0,realy=0,i,rd;

rd=read(event0_fd,ev0,sizeof(structinput_event)*64);

if(rd<sizeof(structinput_event))return0;

for(i=0;i<rd/sizeof(structinput_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;

elseif(ev0[i].type==3&&ev0[i].code==1)

realy=ev0[i].value;

elseif(ev0[i].type==1){

if(ev0[i].code==158){

//ifkeyescthenexit

return0;

}

}elseif(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);

}

return1;

}

intmain(void){

intdone=1;

printf("sizeof(structinput_event)=%d\n",sizeof(structinput_event));

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

if(event0_fd<0)

return-1;

while(done){

printf("beginhandel_event0…\n");

done=handle_event0();

printf("endhandel_event0…\n");

}

if(event0_fd>0){

close(event0_fd);

event0_fd=-1;

}

return0;

}

相关推荐