(/usr/include/linux/input.h)
触摸屏文件数据被封装在input.h文件中的input_event这个结构体中
#include "get_xy.h"
int getxy()
{
//1.打开触摸屏文件
int fd = open(TS_PATH, O_RDWR);
if (fd == -1)
{
perror("open failed!");
return -1;
}
//2.读取触摸屏文件数据
struct input_event xy;
int x, y; //存放点击屏幕的横纵坐标
int flag = 0; //记录当前获取坐标的信息
while (1)
{
read(fd, &xy, sizeof(xy));
if(xy.type == EV_ABS && xy.code == ABS_X)
{
x = xy.value*800/1024;//获取点击的时候X轴坐标的值 (0~1024)--> (0~800)
flag = 1;
}
if(xy.type == EV_ABS && xy.code == ABS_Y)
{
y = xy.value*480/600; //获取点击的时候Y轴坐标的值 (0~600)-->(0~480)
flag = 2;
}
//设置条件:每读取一次完整的坐标,就打印一次坐标
if(flag == 2)
{
printf("(%d,%d)\n", x, y);
flag = 0;
break;//获取一次坐标就跳出循环
}
}
//3.关闭触摸屏文件
close(fd);
return 0;
}
get_xy.h
#ifndef _GET_XY_H
#define _GET_XY_H
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h> //输入子系统的头文件
#define TS_PATH "/dev/input/event0"
int getxy();
#endif