linux 处理键盘 鼠标事件

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

#include 
#include 
#include 
#include 
#include 

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 
#include 
#include 
#include 
#include 

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



你可能感兴趣的:(Linux,鼠标,linux)