如何在linux中控制鼠标移动?

前端之家收集整理的这篇文章主要介绍了如何在linux中控制鼠标移动?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在 Linux中控制鼠标. Xlib似乎有效,但是当我尝试使用OpenCV时,它会继续返回:
  1. Resource temporarily unavailable

所以我决定写“/ dev / psaux”.代码如下:

  1. #include <unistd.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8.  
  9.  
  10. int main() {
  11. unsigned char a[5]={0,0xff,0x28,0xff};
  12. int fp = open ("/dev/psaux",O_WRONLY);
  13. if(!fp)printf("open error:%s\n",strerror(errno));
  14. for(int i = 0; i < 10; i++)
  15. printf("write:%d\t\t%s\n",write(fp,a,5),strerror(errno));
  16. close(fp);
  17. return 0;
  18. }

编译:

  1. gcc my_psaux.c -o my_psaux -std=gnu99 -g

运行并得到

  1. $sudo ./my_psaux
  2. write:5 Success
  3. write:5 Success
  4. write:5 Success
  5. write:5 Success
  6. write:5 Success
  7. write:5 Success
  8. write:5 Success
  9. write:5 Success
  10. write:5 Success
  11. write:5 Success

但鼠标不动.然后我打开一个新的终端,输入“sudo cat / dev / psaux”并运行“my_psaux”.
但我什么也没有.没有写入“/ dev / psaux”?

有人可以帮我吗

如果这不是一个很好的方法来控制鼠标,有人可以告诉我另一个吗?

解决方法

非常感谢@R ..
提醒我一些其他方式,而不是/ dev / psaux

所以我试过/ dev / input / mouse *和/ dev / input / event *

通过使用

  1. cat /proc/bus/input/devices

我得到这个:

  1. I: Bus=0003 Vendor=0461 Product=4d81 Version=0111
  2. N: Name="USB Optical Mouse"
  3. P: Phys=usb-0000:00:1d.0-1/input0
  4. S: Sysfs=/devices/pci0000:00/0000:00:1d.0/usb6/6-1/6-1:1.0/input/input10
  5. U: Uniq=
  6. H: Handlers=mouse2 event10
  7. B: EV=17
  8. B: KEY=70000 0 0 0 0 0 0 0 0
  9. B: REL=143
  10. B: MSC=10

测试后,只有/ dev / input / event10可以工作.代码如下:

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <linux/input.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <sys/time.h>
  9.  
  10. int main() {
  11. struct input_event event,event_end;
  12.  
  13. int fd = open("/dev/input/event10",O_RDWR);
  14. if (fd < 0) {
  15. printf("Errro open mouse:%s\n",strerror(errno));
  16. return -1;
  17. }
  18. memset(&event,sizeof(event));
  19. memset(&event,sizeof(event_end));
  20. gettimeofday(&event.time,NULL);
  21. event.type = EV_REL;
  22. event.code = REL_X;
  23. event.value = 100;
  24. gettimeofday(&event_end.time,NULL);
  25. event_end.type = EV_SYN;
  26. event_end.code = SYN_REPORT;
  27. event_end.value = 0;
  28. for (int i=0; i<5; i++) {
  29. write(fd,&event,sizeof(event));// Move the mouse
  30. write(fd,&event_end,sizeof(event_end));// Show move
  31. sleep(1);// wait
  32. }
  33. close(fd);
  34. return 0;
  35. }

猜你在找的Linux相关文章