歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 用C語言在Linux下獲取鼠標光標的相對位置

用C語言在Linux下獲取鼠標光標的相對位置

日期:2017/3/1 10:38:11   编辑:Linux編程

用C語言在Linux下獲取鼠標光標的相對位置代碼分享:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <linux/input.h>
  4. #include <fcntl.h>
  5. #include <sys/time.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <unistd.h>
  9. int main(int argc,char **argv)
  10. {
  11. int fd, retval;
  12. char buf[6];
  13. fd_set readfds;
  14. struct timeval tv;
  15. //fd = open("/dev/input/mice", O_RDONLY);
  16. if(( fd = open("/dev/input/mice", O_RDONLY))<0)
  17. {
  18. printf("Failed to open \"/dev/input/mice\".\n");
  19. exit(1);
  20. }
  21. else
  22. {
  23. printf("open \"/dev/input/mice\" successfuly.\n");
  24. }
  25. while(1)
  26. {
  27. tv.tv_sec = 5;
  28. tv.tv_usec = 0;
  29. FD_ZERO(&readfds);
  30. FD_SET(fd, &readfds);
  31. retval = select(fd+1, &readfds, NULL, NULL, &tv);
  32. if(retval==0)
  33. printf("Time out!\n");
  34. if(FD_ISSET(fd,&readfds))
  35. {
  36. if(read(fd, buf, 6) <= 0)//終端設備,一次只能讀取一行
  37. {
  38. continue;
  39. }
  40. printf("Button type = %d, X = %d, Y = %d, Z = %d\n", (buf[0] & 0x07), buf[1], buf[2], buf[3]);
  41. }
  42. }
  43. close(fd);
  44. return 0;
  45. }
Copyright © Linux教程網 All Rights Reserved