1. 程式人生 > >通過select較高效的捕獲和處理滑鼠鍵盤事件

通過select較高效的捕獲和處理滑鼠鍵盤事件

select監聽這些檔案描述符的狀態,若有一個發生變化則返回,否則阻塞。

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

int main(int argc,char **argv)    
{    
    int fd, retval;    
    char buf[6];    
    fd_set readfds;    
    struct timeval tv;    

    // 開啟滑鼠裝置    
    fd = open( "/dev/input/mice", O_RDONLY );    
    // 判斷是否開啟成功    
    if(fd<0) {    
        printf("Failed to open \"/dev/input/mice\".\n");    
        exit(1);    
    } else {    
        printf("open \"/dev/input/mice\" successfuly.\n");    
    }    
    
	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;  
	}
	
	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) {    
        // 設定最長等待時間    
        tv.tv_sec = 5;    
        tv.tv_usec = 0;    
    
        FD_ZERO( &readfds );    
        FD_SET( fd, &readfds );    
        FD_SET( fd_kb, &readfds ); 
        FD_SET( fd_mouse, &readfds ); 
    
		int max=fd>fd_kb?(fd>fd_mouse?fd:fd_mouse):(fd_kb>fd_mouse?fd_kb:fd_mouse);
		
        retval = select( max+1, &readfds, NULL, NULL, &tv );//有檔案描述符的狀態發生變化(變為可讀)或者超時和錯誤就返回,否則就阻塞    
        if(retval==0) {    
            printf( "Time out!\n" );    
        }else if(retval<0){
			printf( "error!\n" ); 
		}else{						//判斷是哪個檔案描述符的狀態發生了變化,進行處理即可
		
			//滑鼠位置
			if(FD_ISSET(fd,&readfds)) 
			{    
				// 讀取滑鼠裝置中的資料    
				if(read(fd, buf, 6) <= 0) {    
					continue;    
				}    
				// 打印出從滑鼠裝置中讀取到的資料    
				printf("Button type = %d, X = %d, Y = %d, Z = %d\n", (buf[0] & 0x07), buf[1], buf[2],   buf[3]);    
			}  
			
			//鍵盤點選
			if(FD_ISSET(fd_kb,&readfds)) 
			{    
				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");    
                        }  
  
                    }  
                  
                }     
			}  
			
			//滑鼠點選
			if(FD_ISSET(fd_mouse,&readfds)) 
			{    
				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);  
	close(fd_kb); 
	close(fd_mouse);  	
    return 0;    
}