Linux 模拟触摸滑动以及按下

首先,通过查看手动按下和滑动触摸屏时的内核日志输出:

在这里插入图片描述

然后,我们可以根据上面的日志,在/dev/input/event3(不同平台可能不一样)文件中写入数据以模拟手动按下和滑动触摸屏的效果,代码如下:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <linux/input.h>
#include <errno.h>

#define SILDING_DELAY 10000
#define SILDING_STEP 10

int id = 100;
int TOUCH_FD;

int reportkey(int fd, int type, int code, int32_t value)
{
    struct input_event event;
    event.type = type;
    event.code = code;
    event.value = value;
    gettimeofday(&event.time, 0);
    if (write(fd, &event, sizeof(struct input_event)) < 0)
    {
        perror("report key error!\n");
        return -1;
    }
    return 0;
}

//构造点击事件
void LCD_Touch_Pressed(int posx,int posy,int sec)
{
    reportkey(TOUCH_FD,EV_ABS,ABS_MT_SLOT,0);
    reportkey(TOUCH_FD,EV_ABS,ABS_MT_TRACKING_ID,id);
    reportkey(TOUCH_FD,EV_ABS,ABS_MT_POSITION_X,posx);
    reportkey(TOUCH_FD,EV_ABS,ABS_MT_POSITION_Y,posy);
    reportkey(TOUCH_FD,EV_ABS,ABS_MT_TRACKING_ID,0);
    reportkey(TOUCH_FD,EV_SYN,EV_SYN,EV_SYN);

    reportkey(TOUCH_FD,EV_ABS,ABS_PRESSURE,200);
    reportkey(TOUCH_FD,EV_ABS,ABS_MT_TRACKING_ID,-1);
    reportkey(TOUCH_FD,EV_SYN,EV_SYN,EV_SYN);
}

//构造滑动事件
void LCD_Press_Silding(int sx,int sy,int ex,int ey ,int sec)
{
    int nextPosX,nextPosY;
    if(ex == sx && ey==sy) // the same two points
    {
        LCD_Touch_Pressed(sx,sy,sec);
        return ;
    }

    reportkey(TOUCH_FD,EV_ABS,ABS_MT_SLOT,0);
    reportkey(TOUCH_FD,EV_ABS,ABS_MT_TRACKING_ID,id);
    reportkey(TOUCH_FD,EV_ABS,ABS_MT_POSITION_X,sx);
    reportkey(TOUCH_FD,EV_ABS,ABS_MT_POSITION_Y,sy);
    reportkey(TOUCH_FD,EV_ABS,ABS_MT_TRACKING_ID,0);

    reportkey(TOUCH_FD,EV_ABS,ABS_PRESSURE,200);
    reportkey(TOUCH_FD,EV_SYN,EV_SYN,EV_SYN);
    usleep(SILDING_DELAY);
    /**Report pressed-down event END----*/
    nextPosX = sx;
    nextPosY = sy;
    if(ex == sx) // vertical Line
    {
        nextPosY = ey > sy ? (nextPosY + SILDING_STEP) : (nextPosY - SILDING_STEP);
        while (abs(nextPosY-sy)< abs(ey-sy))
        {
            reportkey(TOUCH_FD,EV_ABS,ABS_MT_POSITION_X,ex);
            reportkey(TOUCH_FD,EV_ABS,ABS_MT_POSITION_Y,nextPosY);
            reportkey(TOUCH_FD,EV_SYN,EV_SYN,EV_SYN);
            usleep(SILDING_DELAY);
            nextPosY = ey > sy ? (nextPosY + SILDING_STEP) : (nextPosY - SILDING_STEP);
        }
    }
    else if(ey == sy) // horizontal Line
    {
        nextPosX = ex > sx ? (nextPosX + SILDING_STEP) : (nextPosX - SILDING_STEP);
        while (abs(nextPosX-sx)< abs(ex-sx))
        {
            reportkey(TOUCH_FD,EV_ABS,ABS_MT_POSITION_X,nextPosX);
            reportkey(TOUCH_FD,EV_ABS,ABS_MT_POSITION_Y,ey);
            reportkey(TOUCH_FD,EV_SYN,EV_SYN,EV_SYN);
            usleep(SILDING_DELAY);
            nextPosX = ex > sx ? (nextPosX + SILDING_STEP) : (nextPosX - SILDING_STEP);
        }
    }
    reportkey(TOUCH_FD,EV_ABS,ABS_MT_TRACKING_ID,-1);
    reportkey(TOUCH_FD,EV_SYN,EV_SYN,EV_SYN);
}

int main()
{

    TOUCH_FD = open("/dev/input/event3",O_RDWR);
    if(TOUCH_FD<=0)
    {
        printf("%s\n", strerror(errno));
        return 0;
    }

    while (1)
    {
        id++;

        sleep(3);
        LCD_Press_Silding(600,300,500,300 ,100);

        id++;
        sleep(3);
        LCD_Touch_Pressed(155,261,500);

    }

    close(TOUCH_FD);
    return 0;
}

在这里插入图片描述
最后,大家可以查看自己设备的界面是不是有变化呢?

参考:https://blog.csdn.net/weixin_36437103/article/details/116876467
需要源代码的可自行下载:点击跳转

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值