Android 中input学*--键盘/触摸屏/手柄。。。模拟器

发布于:2021-06-15 20:21:13

上一篇说过/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
", 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
", 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
", 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
", 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
", strerror(errno));
}
}

int main()
{
init_touch();
TouchEvent(1,200,400);
TouchEvent(0,200,400);
cleanup_touch();
return 0;
}


?


?

相关推荐

最新更新

猜你喜欢