Advertisement
metalx1000

Read Mouse Inputs in C gcc

Oct 20th, 2017
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4.  
  5. int main(){
  6.   int fd, bytes;
  7.   unsigned char data[3];
  8.  
  9.   //set mouse device location
  10.   const char *mouseDevice = "/dev/input/mice";
  11.  
  12.   // Open Mouse as Read Only
  13.   fd = open(mouseDevice, O_RDONLY);
  14.   if(fd == -1){
  15.     printf("ERROR Opening %s\n", mouseDevice);
  16.     printf("You may not have permission to read from this device.\n");
  17.     return -1;
  18.   }
  19.  
  20.   int l, m, r;
  21.   signed char x, y;
  22.   while(1){
  23.     // Read Mouse    
  24.     bytes = read(fd, data, sizeof(data));
  25.  
  26.     if(bytes > 0){
  27.       l = data[0] & 0x1;
  28.       r = data[0] & 0x2;
  29.       m = data[0] & 0x4;
  30.  
  31.       x = data[1];
  32.       y = data[2];
  33.       /*action if left mouse button is clicked
  34.       if(l == 1){
  35.         printf("Left Mouse Button Clicked.\n");
  36.       }
  37.       */
  38.       printf("x=%d, y=%d, l=%d, m=%d, r=%d\n", x, y, l, m, r);
  39.  
  40.     }  
  41.   }
  42.   return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement