Linux和android下测试键盘和触摸屏

在Linux或者Android-x86系统下,会用到测试键盘、鼠标、触摸屏等各种输入设备的功能,那么下面的这段代码是个好的选择。首先编写了个Linux输入设备的测试小程序来检测问题所在,总算也小有成就。具体输入设备的路径,大家可以用cat /proc/bus/input/devices查看自己机器的设备文件。
       检测按键的程序如下:
  1. #include <stdio.h> 
  2. #include <sys/types.h> 
  3. #include <sys/stat.h> 
  4. #include <fcntl.h> 
  5. #include <linux/input.h> 
  6. #define NOKEY 0 
  7.  
  8. int main() 
  9.     int keys_fd;                     //按键句柄  
  10.         char ret[2];  
  11.     struct input_event t; 
  12.        
  13.         keys_fd = open("/dev/input/event0", O_RDONLY); 
  14.     if(keys_fd<=0) 
  15.     { 
  16.                 printf("open /dev/input/event0 device error!\n"); 
  17.         return 0; 
  18.     } 
  19.  
  20.     while(1) 
  21.     {    
  22.                 if(read(keys_fd,&t,sizeof(t))==sizeof(t)) { 
  23.             if(t.type==EV_KEY)          //获取的是按键消息 
  24.             if(t.value==0 || t.value==1)    //返回值是1或者0 
  25.                 printf("key %d %s\n",t.code,(t.value)?"Pressed":"Released");     //1表按下,0表弹起 
  26.         } 
  27.     }    
  28.     close(keys_fd); 
  29.      
  30.         return 0; 
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#define NOKEY 0

int main()
{
	int keys_fd;	                 //按键句柄	
        char ret[2]; 
	struct input_event t;
      
        keys_fd = open("/dev/input/event0", O_RDONLY);
	if(keys_fd<=0)
	{
                printf("open /dev/input/event0 device error!\n");
		return 0;
	}

	while(1)
	{	
                if(read(keys_fd,&t,sizeof(t))==sizeof(t)) {
		    if(t.type==EV_KEY)			//获取的是按键消息
			if(t.value==0 || t.value==1)    //返回值是1或者0
   			    printf("key %d %s\n",t.code,(t.value)?"Pressed":"Released");     //1表按下,0表弹起
  		}
	}	
	close(keys_fd);
	
        return 0;
}

       执行结果如下:

        检测触摸屏的程序如下:

  1. #include <stdio.h> 
  2. #include <sys/types.h> 
  3. #include <sys/stat.h> 
  4. #include <fcntl.h> 
  5. #include <linux/input.h> 
  6.  
  7. int main() 
  8.         int keys_fd;     
  9.             char ret[2];  
  10.         struct input_event t; 
  11.        
  12.                 keys_fd = open("/dev/input/event1", O_RDONLY);    //打开TP设备 
  13.             if(keys_fd<=0){ 
  14.                     printf("open /dev/input/event0 device error!\n"); 
  15.             return 0; 
  16.         } 
  17.  
  18.         while(1) 
  19.         {    
  20.                        if(read(keys_fd,&t,sizeof(t))==sizeof(t)) { 
  21.                     if (t.type == EV_KEY){ 
  22.                             printf("  type: EV_KEY, event = %s, value = %d \r\n",  
  23.                                 t.code == BTN_TOUCH ? "BTN_TOUCH" : "Unkown", t.value);  
  24.                     } 
  25.                     else if(t.type == EV_ABS){ 
  26.                             printf("  type: EV_ABS, event = %s, value = %d \r\n",  
  27.                            t.code == ABS_X ? "ABS_X" :  
  28.                            t.code == ABS_Y ? "ABS_Y" :  
  29.                            t.code == ABS_PRESSURE ? "ABS_PRESSURE" :"Unkown", t.value);  //该处使用了一些特殊的用法::  
  30.                     } 
  31.             } 
  32.         }    
  33.         close(keys_fd); 
  34.      
  35.             return 0; 
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>

int main()
{
		int keys_fd;	
        	char ret[2]; 
		struct input_event t;
      
       	        keys_fd = open("/dev/input/event1", O_RDONLY);    //打开TP设备
     		if(keys_fd<=0){
                	printf("open /dev/input/event0 device error!\n");
			return 0;
		}

		while(1)
		{	
               	       if(read(keys_fd,&t,sizeof(t))==sizeof(t)) {
			    	if (t.type == EV_KEY){
			    			printf("  type: EV_KEY, event = %s, value = %d \r\n", 
                          		t.code == BTN_TOUCH ? "BTN_TOUCH" : "Unkown", t.value); 
			    	}
			    	else if(t.type == EV_ABS){
			    			printf("  type: EV_ABS, event = %s, value = %d \r\n", 
                           t.code == ABS_X ? "ABS_X" : 
                           t.code == ABS_Y ? "ABS_Y" : 
                           t.code == ABS_PRESSURE ? "ABS_PRESSURE" :"Unkown", t.value);  //该处使用了一些特殊的用法:: 
  			    	}
			}
		}	
		close(keys_fd);
	
        	return 0;
}

       执行结果如下:


关于Input设备,说明:

(1)ls -l /dev/input,得到设备名称和属性,注意此处没有input号这种Input层分配的内容,以event为主。如:

# ls -l /dev/input
crw-rw---- root     input     13,  66 1970-01-01 00:00 event2
crw-rw---- root     input     13,  33 1970-01-01 00:00 mouse1
crwxrwxrwx root  input     13,  65 1970-01-01 00:00 event1
crw-rw---- root     input     13,  32 1970-01-01 00:00 mouse0
crw-rw---- root     input     13,  64 1970-01-01 00:00 event0
crw-rw---- root     input     13,  63 1970-01-01 00:00 mice

如果这么些设备中无法确认哪个是目前在用的设备?可以采用这种方式:cat他们,然后操作鼠标或者键盘,哪个输出乱码就是用的哪个。

(2)cat /proc/bus/input/devices,主要信息是:

N: Name="s3c-keypad-rev0000"
P: Phys=s3c-keypad/input0
S: Sysfs=/class/input/input0
H: Handlers=kbd event0

N: Name="S3C TouchScreen"
P: Phys=input(ts)
S: Sysfs=/class/input/input1
U: Uniq=
H: Handlers=kbd mouse0 event1

N: Name="ADXL34x accelerometer"
P: Phys=1-0053/input0
S: Sysfs=/class/input/input2
U: Uniq=
H: Handlers=mouse1 event2

分配的Input节点全在Sysfs上,真正的设备dev在Handlers上。

(3)ls -l /sys/class/input,类设备信息:

drwxr-xr-x root     root              1970-01-01 00:00 mice
drwxr-xr-x root     root              1970-01-01 00:00 input0
lrwxrwxrwx root     root            1970-01-01 00:04 event0 -> input0/event0
drwxr-xr-x root     root              1970-01-01 00:00 input1
lrwxrwxrwx root     root            1970-01-01 00:04 mouse0 -> input1/mouse0
lrwxrwxrwx root     root             1970-01-01 00:04 event1 -> input1/event1
drwxr-xr-x root     root              1970-01-01 00:00 input2
lrwxrwxrwx root     root             1970-01-01 00:04 mouse1 -> input2/mouse1
lrwxrwxrwx root     root             1970-01-01 00:04 event2 -> input2/event2

参考原文:http://wangliping.net/linux-android-x86-test-keyboard-mouse-touch-screen

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值