上一篇说过/dev/input/下的设备是各种输入设备
怎么用自己的设备/程序模拟这些输入设备呢?------直接写
比如下面这段代码就可以模拟压下,放开事件
怎么扩展,留备后用
static char TOUCH_DEVICE[256] = "/dev/input/event3";
int touchfd = -1;
static void init_touch()
{
if((touchfd = open(TOUCH_DEVICE, O_RDWR)) == -1)
{
printf("cannot open touch device %s\n", TOUCH_DEVICE);
}
}
static void cleanup_touch()
{
if(touchfd != -1)
{
close(touchfd);
}
}
void TouchEvent(int down, int x, int y)
{
struct input_event ev;
memset(&ev, 0, sizeof(ev));
gettimeofday(&ev.time,0);
ev.type = EV_KEY;
ev.code = BTN_TOUCH;
ev.value = down;
if(write(touchfd, &ev, sizeof(ev)) < 0)
{
printf("write event failed, %s\n", strerror(errno));
}
gettimeofday(&ev.time,0);
ev.type = EV_ABS;
ev.code = ABS_X;
ev.value = x;
if(write(touchfd, &ev, sizeof(ev)) < 0)
{
printf("write event failed, %s\n", strerror(errno));
}
gettimeofday(&ev.time,0);
ev.type = EV_ABS;
ev.code = ABS_Y;
ev.value = y;
if(write(touchfd, &ev, sizeof(ev)) < 0)
{
printf("write event failed, %s\n", strerror(errno));
}
gettimeofday(&ev.time,0);
ev.type = EV_SYN;
ev.code = 0;
ev.value = 0;
if(write(touchfd, &ev, sizeof(ev)) < 0)
{
printf("write event failed, %s\n", strerror(errno));
}
}
int main()
{
init_touch();
TouchEvent(1,200,400);
TouchEvent(0,200,400);
cleanup_touch();
return 0;
}

本文探讨了在Android中如何模拟键盘、触摸屏和手柄等输入设备,并通过举例介绍如何利用代码模拟输入事件,包括按下和释放操作。同时,文章提及了输入设备的扩展可能性。
1906

被折叠的 条评论
为什么被折叠?



