1.读取键盘的值,进行相应的处理。

#include <stdio.h>
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
        int fd_kb;
	
        struct input_event event_kb;

        fd_kb = open("/dev/input/event1", O_RDONLY); //键盘输入
        if(fd_kb <= 0)
        {
                printf("open device error\n");
                return 0;
        }

        while(1)
        {
                if(read(fd_kb, &event_kb, sizeof(event_kb)) == sizeof(event_kb))
                {
                    if (event_kb.type == EV_KEY)
					{
                        //if (event_kb.value == 0 || event_kb.value == 1)//1表示按下,0表示释放,会检测到两次
						if (event_kb.value == 1)//键按下
                        {
                                //printf("key %d %s\n", event_kb.code, (event_kb.value) ? "Pressed" : "Released");
                                if(event_kb.code == KEY_ESC)
                                        break;
								if(event_kb.code == KEY_Q)
									printf("q\n");                                 
								if(event_kb.code == KEY_W)
                                    printf("w\n");  
								if(event_kb.code == KEY_E)
                                    printf("e\n");  
								if(event_kb.code == KEY_R)
                                    printf("r\n");  
                        }

					}
				
                }
				
				
				
        }
        close(fd_kb);
        return 0;
}

2.获取并处理鼠标左键按下,右键按下,滚轮按下,滚轮上下滑动事件


#include <stdio.h>
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
		int fd_mouse;
		
		struct input_event event_mouse;
		
		fd_mouse = open("/dev/input/event4", O_RDONLY); //鼠标输入
        if(fd_mouse <= 0)
        {
                printf("open device error\n");
                return 0;
        }
		
        while(1)
        {			
				if(read(fd_mouse, &event_mouse, sizeof(event_mouse)) == sizeof(event_mouse))
                {                 					
 					if(event_mouse.type = EV_REL)
					{
						if(event_mouse.code == REL_WHEEL)		
							printf("REL_WHEEL %d\n", event_mouse.value);//-1表示下滑,1表示上滑
					} 	


	
					if(event_mouse.code == BTN_LEFT && event_mouse.value==1)//左键按下,1表示按下,0表示释放。不然会检测到两次
						printf("left down: %d\n", event_mouse.code);
						
					if(event_mouse.code == BTN_RIGHT && event_mouse.value==1)
						printf("right down: %d\n", event_mouse.code);
						
					if(event_mouse.code == BTN_MIDDLE && event_mouse.value==1)
						printf("middle down: %d\n", event_mouse.code);
		
	
                }
        }
		close(fd_mouse);
        return 0;
}


重要的参考资料:

1.http://staratsky.iteye.com/blog/1734631

2.http://blog.chinaunix.net/uid-20666855-id-4905405.html

3.http://blog.csdn.net/shijian408/article/details/26578007



GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐