android 按键精灵,通过getevent记录后发出

最近需要写这个程序。网上没看着太好的,还是自己动手写一个。大概的思想是通过getevent获得屏幕移动的数据,将数据以2进制格式保存,用c语言读取后写入到屏幕。

首先用android提供的getevent进行事件记录,直接输入getevent可以看到你的屏幕是哪个event。

然后getevent -t /dev/input/eventX,输入完之后去操作屏幕,录入你想录的操作,输出的格式是

时间 数字 数字 数字

不同的板子的时间这一块不太一样,要具体问题具体分析。一般来说是形如[10.123]。这里不能直接转换成sendevent的脚本,因为sendevent中,那三个数字通过参数进入,需要用atoi转换成整数写入设备,而event在数秒内就可以有数百个,这样的字符串转整数通常是手机平台不能流畅完成的。所以用下面一段python3代码:

import struct
time0=0
rectimes=0
lastrec=0
with open("e:\\ev1.txt",'r') as f, open("e:\\rec2.touch",'wb') as wf, open("e:\\rec2.index",'wb') as id:
    for line in f:
        line=line.split(' ')
        time1=int(float(line[0][1:-1])*1000000) 
        print(line)
        if (time1-time0)>0:
            #write sleeptime, and readtimes
            if rectimes==0:
                id.write(struct.pack('ii',0,rectimes-lastrec))
            else:
                id.write(struct.pack('ii',time1-time0,rectimes-lastrec))
                
            lastrec=rectimes
        time0=time1
        rectimes+=1
        #python -> C struct
        # 2byte __u16 type
        # 2byte __u16 code
        # 4byte __s32 value
        wf.write(struct.pack("HHI",int(line[1],16),int(line[2],16),int(line[3].strip(),16)))

 然后写了一段C代码在板子上跑,这里要交叉编译

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/ioctl.h>
//#include <linux/input.h> // this does not compile
#include <errno.h>
#include <asm/types.h>

struct input_event {
	struct timeval time;
	__u16 type;
	__u16 code;
	__s32 value;
};



int main(int argc, char *argv[])
{
    int fd,fi,ft;
    int ret;
    int version;
    struct input_event event;
    if(argc != 3) {
        fprintf(stderr, "use: %s device filename\n", argv[0]);
        return 1;
    }
    char TouchFile[40],IndexFile[40];
    strcpy(TouchFile,argv[2]);
    strcpy(IndexFile,argv[2]);
    strcat(TouchFile,".touch");
    strcat(IndexFile,".index");
    ft = open(TouchFile,O_RDONLY);
    fi = open(IndexFile,O_RDONLY);
    fd = open(argv[1], O_RDWR);

while(1)
{
	struct input_event event;
	memset(&event, 0, sizeof(event));
	    int sleeptime;
	    if(read(fi,(char*)&sleeptime,4)==0)
	    {
	    	while(read(ft,&(event.type),8))
	    	{
	    		write(fd, &event, sizeof(event));
	    	}
	    	return 0;
	    }
//	    printf("sleeptime:%d\n",sleeptime);
	    
	    int readtimes;
	    read(fi,(char*)&readtimes,4);
//	    printf("readtimes:%d\n",readtimes);
	    int i=0;
	    char buffer[8];
	    for(;i<readtimes;++i)
	    {
	    	read(ft,&(event.type),8);
	    	write(fd, &event, sizeof(event));
	    }
	    usleep(sleeptime);
}

    return 0;
}

 

posted @ 2013-08-01 17:06  张拯  阅读(1054)  评论(1编辑  收藏  举报