2017-02-15 297 views
0

我正在嘗試從/dev/input/mice文件中讀取鼠標事件。我能夠解析三個字節的鼠標輸入以獲得三個按鈕狀態以及X和Y座標中的增量。但是,向上滾動時的鼠標輸入與向下滾動時的輸入相同。如何區分向下滾動事件中的向上滾動事件?有沒有任何ioctls可以做任何必要的配置,以便我在這兩個事件上從鼠標得到不同的輸入?如何在c中獲取鼠標滾輪事件

以下是一個簡單的程序,用於查看鼠標事件發生時鼠標的輸入。向上滾動並向下滾動事件會導致該程序打印相同的輸出(即8 0 0)。

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <stdbool.h> 
#include <unistd.h> 
#include <fcntl.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <errno.h> 

int main(void) { 

    int mouse_fd = open("/dev/input/mice", O_RDONLY | O_NONBLOCK); 
    signed char input[4]; 
    ssize_t rd_cnt; 

    if(mouse_fd < 0) 
    { 
     perror("Could not open /dev/input/mice"); 
     exit(EXIT_FAILURE); 
    } 

    while(true) 
    { 
     errno = 0; 
     rd_cnt = read(mouse_fd, input, 4); 
     if(rd_cnt <= 0 && errno != EAGAIN) 
     { 
      perror("Mouse read error:"); 
      exit(EXIT_FAILURE); 
     } 
     else 
     { 
      for(int i = 0; i < rd_cnt; i++) 
      { 
       printf("%d", input[i]); 
       if(i == rd_cnt - 1) 
       { 
        printf("\n"); 
       } 
       else 
       { 
        printf("\t"); 
       } 
      } 
     } 
    } 

    return 0; 
} 
+0

我們有類似* libinput *的東西嗎?一個例子是X的庫,它可以完成所有的工作。你可以看看它的來源。 – 0andriy

+0

您是否想過使用SDL2進行鼠標輸入? –

+0

我最近發現我在找的是如何使用輸入子系統來獲取鼠標事件。我的問題解決了。無論如何,謝謝你的幫助。 –

回答

0

另一種方法是使用SDL2。

我已經設法將鼠標輸入與SDL讀取的示例混合在一起,因此請從中獲取您喜歡的內容。

#include <SDL2/SDL.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <stdbool.h> 


char* itoa(int i, char b[]){ 
    char const digit[] = ""; 
    char* p = b; 
    if(i<0){ 
     *p++ = '-'; 
     i *= -1; 
    } 
    int shifter = i; 
    do{ //Move to where representation ends 
     ++p; 
     shifter = shifter/10; 
    }while(shifter); 
    *p = '\0'; 
    do{ //Move back, inserting digits as u go 
     *--p = digit[i%10]; 
     i = i/10; 
    }while(i); 
    return b; 
} 

int main() 
{ 
    //initialize the window 
    bool quit = false; 
    SDL_Init(SDL_INIT_VIDEO); 
    SDL_Window* window = SDL_CreateWindow("Mouse Events", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0); 
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0); 

    // the event that will occur when a mouse event happens 
    SDL_Event event; 
    while(!quit) 
    { 

     while (SDL_PollEvent(&event)) //returns one is there is a pending event (if an event happens) 
     { 
      switch(event.type) 
      { 
       case SDL_QUIT: //if the window is exited 
        quit = true; 
        break; 

       case SDL_MOUSEBUTTONDOWN: 
        switch (event.button.button) 
        { 
         case SDL_BUTTON_LEFT: 
          SDL_ShowSimpleMessageBox(0, "Click", "Left button was pressed!", window); 
          break; 
         case SDL_BUTTON_RIGHT: 
          SDL_ShowSimpleMessageBox(0, "Click", "Right button was pressed!", window); 
          break; 
        } 
        break; 
       case SDL_MOUSEWHEEL: 
        if(event.wheel.y == -1) //negative means the scroll wheel has gone away from the user 
        { 
         SDL_ShowSimpleMessageBox(0, "Wheel Event", "You rolled away from yourself!", window); 
        } else if (event.wheel.y == 1) //vice-versa 
        { 
         SDL_ShowSimpleMessageBox(0, "Wheel Event", "You rolled towards yourself!", window); 
        } 

      } 

     } 

     //do some SDL cleanup 
     SDL_Rect dstrect = { 288, 208, 64, 64 }; 

     SDL_RenderClear(renderer); 
     SDL_RenderPresent(renderer); 
    } 

} 

event.wheel類型可以在這裏找到:https://wiki.libsdl.org/SDL_MouseWheelEvent

希望這是一些使用的爲您服務!

如果您不想使用SDL2,可能需要查看庫的來源以瞭解其功能。