测试触摸屏功能
在Secure CRT上输入命令:cat /dev/input/event0
回车,然后点击屏幕,看看是否有反应
触摸屏原理
根据手指按下的位置,产生x、y坐标
触摸屏坐标获取、和其功能程序实现原理
- 如何产生x轴、y轴坐标值
- 如何判断手指左右滑动 3. 如何判断手指放大或缩小
- 如何只获取一个点的坐标
- 页面跳转:在相同位置实现不同功能
输入子系统模型
头文件:#include <linux/input.h>
struct input_event
{
struct timeval time; //输入事件的时间
_u16 type; //输入事件的类型
_u16 code; //输入事件的类型的编码
_s32 value; //输入事件的类型的编码的值
};
//输入事件的类型:
EV_KEY //键盘
EV_REL //鼠标
EV_ABS //触摸屏
...
//输入事件的类型的编码:
ABS_X //触摸屏的x轴的值
ABS_Y //触摸屏的y轴的值
BTN_TOUCH //触摸值,按下1,松开0
...
//输入事件的类型的编码的值:你判断的是什么类型,什么编码,那么它的value值就是多少
示例代码
//头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <linux/input.h>
int ts_fd=-1;
void init_ts(void);//打开触摸屏
void gets_pos(int *x,int *y); //获取触摸屏位置
int main(int argc, char const *argv[])
{
init_ts();
int x,y;
while(1) //触摸屏
{
gets_pos(&x,&y);
printf("(%d,%d)\n",x,y);
if(x>0 && x<400 &&y>0 && y <480)
{
printf("左边\n");
}
else if(x>400 && x<800 && y>0 && y<480)
{
printf("右边\n");
}
}
close(ts_fd);
return 0;
}
void init_ts(void)
{
//打开ts,路径:/dev/input/event0
ts_fd = open("/dev/input/event0",O_RDWR);
if (ts_fd < 0)
{
perror("open ts_fd failed");//打印错误信息
return;
}
}
//获取x,y坐标
void gets_pos(int *x,int *y)
{
//读取触摸屏数据,获取坐标
struct input_event buf;
while(1)
{
read(ts_fd,&buf,sizeof(buf));
if(buf.type == EV_ABS) //触摸屏事件
{
if(buf.code == ABS_X) //x轴
{
*x = buf.value*800/1024;
}
if(buf.code == ABS_Y) //Y轴
{
*y = buf.value*480/600;
}
}
//判断手指松开
if(buf.type == EV_KEY && buf.code == BTN_TOUCH && buf.value == 0) break;
}
}