/******************************************************************* * Linux Read input device demo * Description: * This article is mainly to understand the previous colleague wrote input device a demo program. * 2016-3-24 Shenzhen Nanshan Ping Shan village Zengjianfeng **************************************************************** **/#include<stdio.h>#include<unistd.h>#include<sys/types.h>#include<fcntl.h>#include<linux/input.h>intMainintargcChar**argv) { intFD; structinput_event ev; //Judging Parameters if(ARGC <2) {printf ("Usage:%s <input device>\n", argv[0]); return 0; } //turn on the deviceFD = open (argv[1], O_RDWR); if(FD <0) {printf ("Open%s", argv[1]); Fflush (stdout); Perror (" "); return 0; } //Loop Read while(1) { //reading DataRead (FD, &ev,sizeof(structinput_event)); //Print the current trigger typeprintf"ev = =%x \ n", Ev.type);
Switch(ev.type) { Caseev_syn:printf ("-------------------------\ n"); Break; //Key Caseev_key:printf ("key down/up:%d \ n", Ev.code); Break; //Mouse Caseev_rel:printf ("Mouse:"); if(Ev.code = =rel_x) {printf ("X--%d\n", Ev.value); } Else if(Ev.code = =rel_y) {printf ("y--%d\n", Ev.value); } Break; //Touch Screen Caseev_abs:printf ("TS:"); if(Ev.code = =abs_x) {printf ("X--%d\n", Ev.value); } Else if(Ev.code = =abs_y) {printf ("y--%d\n", Ev.value); } Else if(Ev.code = =abs_pressure) {printf ("Pressure:%d\n", Ev.value); } Break; }} close (FD); return 0;}
Linux Read input device demo