#include <stdio.h>
#include <linux/input.h>
#include <fcntl.h>
#include <stdint.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#define KEY_ENENT_DEV1_NAME		"/dev/input/event1"
#define MICE_EVENT_NAME			"/dev/input/mice"
#define unsigned short   		__u16
#define signed int				__s32

typedef enum{
	No_Click,
	Left_Click,
	Right_Click,
	Left_Right_Click,
	Middle_Click,
	Middle_Left_Click,
	Middle_Right_Click,
	All_Click,
	StateCount
}MouceClickState;
char *clickState[StateCount] = {
		"No Click",
		"Left Click",
		"Right Click",
		"Left Right Click",
		"Middle Click",
		"Middle Left Click",
		"Middle Right Click",
		"All Click"
};
#define 	THOUSAND				1000
#define 	LONGPRESSTIME			1500

int sysKeyAndMouseScan(void){
	int ret = -1;
	int key_fd = 0;
	int mice_fd = 0;
	int max = 0;
	int absX = 0;
	int absY = 0;
	int lock = 0;//use to lock the origin point
	struct input_event key_event ;
	char miceBuf[6] = {0};
	struct timeval tvStart,tvStop,tvAlarm;
	fd_set readfds;
	long pressTime = 0;
	key_fd = open(KEY_ENENT_DEV1_NAME,O_RDONLY);
	if(key_fd <= 0){
		printf("open /dev/input/event1 device error!\n");
		return ret;
	}
	mice_fd = open(MICE_EVENT_NAME,O_RDONLY);
	if(mice_fd <= 0){
			printf("open /dev/input/mice device error!\n");
			return ret;
	}
	max = ((key_fd > mice_fd)?(key_fd):(mice_fd));
	tvAlarm.tv_sec = 5;
	tvAlarm.tv_usec = 0;
	while(1){
		FD_ZERO(&readfds);
		FD_SET(key_fd,&readfds);
		FD_SET(mice_fd,&readfds);
		if((ret = select(max+1,&readfds,NULL,NULL,&tvAlarm)) < -1){
			continue;
		}else if(ret == 0){
			//printf("TimeOut\n");
			continue;
		}else{
			if(FD_ISSET(key_fd,&readfds)){
				ret = read(key_fd,&key_event,sizeof(key_event));
				if(ret){
					if(key_event.type == EV_KEY ){
						//printf("%d ",key_event.value);
						if(key_event.value == 0){
							tvStop.tv_sec = key_event.time.tv_sec;
							tvStop.tv_usec = key_event.time.tv_usec;
							printf("\trealeased !\n");
							pressTime = (tvStop.tv_sec-tvStart.tv_sec)*THOUSAND+(tvStop.tv_usec-tvStart.tv_usec)/THOUSAND;
							printf("press Time: %ld ms ->",pressTime);
							if(pressTime > LONGPRESSTIME){
								printf(" Long Press!\n");
							}else{
								printf(" Short Press!\n");
							}
						}else if(key_event.value == 1){
							tvStart.tv_sec = key_event.time.tv_sec;
							tvStart.tv_usec = key_event.time.tv_usec;
							printf("\tpressed!\n");
						}else if(key_event.value == 2){
							continue;
						}else{
							printf("other");
						}
						if(key_event.code == KEY_ESC)
						{
							break;
						}
					}

				}
			}
			if(FD_ISSET(mice_fd,&readfds)){

				if((ret = read(mice_fd,miceBuf,sizeof(miceBuf))) <= 0){
					continue;
				}
				else{
					switch(miceBuf[0] & 0x07){
						case No_Click:
							absX += miceBuf[1];
							absY += miceBuf[2];
							printf("%s\tabsX:%d\tabsY:%d\n",clickState[No_Click],absX,absY);
							break;
						case Left_Click:
							if(lock == 0){ //use to lock the origin point
								lock = 1;
								absX = 0;
								absY = 0;
							}
							else{
								absX += miceBuf[1];
								absY += miceBuf[2];
							}
							printf("%s\tabsX:%d\tabsY:%d\n",clickState[Left_Click],absX,absY);
							break;
						case Right_Click:
							absX += miceBuf[1];
							absY += miceBuf[2];
							printf("%s\tabsX:%d\tabsY:%d\n",clickState[Right_Click],absX,absY);
							break;
						case Left_Right_Click:
							absX += miceBuf[1];
							absY += miceBuf[2];
							printf("%s\tabsX:%d\tabsY:%d\n",clickState[Left_Right_Click],absX,absY);
							break;
						case Middle_Click:
							absX += miceBuf[1];
							absY += miceBuf[2];
							printf("%s\tabsX:%d\tabsY:%d\n",clickState[Middle_Click],absX,absY);
							break;
						case Middle_Left_Click:
							absX += miceBuf[1];
							absY += miceBuf[2];
							printf("%s\tabsX:%d\tabsY:%d\n",clickState[Middle_Left_Click],absX,absY);
							break;
						case Middle_Right_Click:
							absX += miceBuf[1];
							absY += miceBuf[2];
							printf("%s\tabsX:%d\tabsY:%d\n",clickState[Middle_Right_Click],absX,absY);
							break;
						case All_Click:
							absX += miceBuf[1];
							absY += miceBuf[2];
							printf("%s\tabsX:%d\tabsY:%d\n",clickState[All_Click],absX,absY);
							break;
						default:break;
					}
				}
			}
		}
	}

	close(mice_fd);
	close(key_fd);
	return ret;
}
int main(int argc, char **argv) {
	printf("---This is a key and mouse event test!---\n");

	sysKeyAndMouseScan();

	printf("exit");
	return 0;
}

实现的功能:

1、键盘和鼠标检测

2、按键时长检测

3、鼠标移动绝对位置检测

(第一次鼠标左击确定原点位置)

Logo

更多推荐