当前位置: 首页 > 面试题库 >

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

段干兴业
2023-03-14
问题内容

我尝试在Linux中控制鼠标。Xlib似乎可以工作,但是当我尝试将其与OpenCV一起使用时,它会不断返回:

Resource temporarily unavailable

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

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


int main() {
    unsigned char a[5]={0, 0xff, 0, 0x28, 0xff};
    int fp = open ("/dev/psaux", O_WRONLY);
    if(!fp)printf("open error:%s\n", strerror(errno));
    for(int i = 0; i < 10; i++)
        printf("write:%d\t\t%s\n", write(fp, a, 5), strerror(errno));
    close(fp);
    return 0;
}

用以下命令编译:

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

运行并获得

$sudo ./my_psaux 
write:5    Success
write:5    Success
write:5    Success
write:5    Success
write:5    Success
write:5    Success
write:5    Success
write:5    Success
write:5    Success
write:5    Success

但是鼠标不会移动。然后,我打开一个新终端,输入“ sudo cat / dev / psaux”并运行“ my_psaux”。但是我什么都没有。
什么都没有写到“ / dev / psaux”中?

有人可以帮我吗?

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


问题答案:

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

于是,我就/dev/input/mouse*/dev/input/event*

通过使用

cat /proc/bus/input/devices

我得到这个:

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

经过测试,仅/dev/input/event10适用。代码如下:

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

int main() {
  struct input_event event, event_end;

  int fd = open("/dev/input/event10", O_RDWR);
  if (fd < 0) {
    printf("Errro open mouse:%s\n", strerror(errno));
    return -1;
  }
  memset(&event, 0, sizeof(event));
  memset(&event, 0, sizeof(event_end));
  gettimeofday(&event.time, NULL);
  event.type = EV_REL;
  event.code = REL_X;
  event.value = 100;
  gettimeofday(&event_end.time, NULL);
  event_end.type = EV_SYN;
  event_end.code = SYN_REPORT;
  event_end.value = 0;
  for (int i=0; i<5; i++) {
    write(fd, &event, sizeof(event));// Move the mouse
    write(fd, &event_end, sizeof(event_end));// Show move
    sleep(1);// wait
  }
  close(fd);
  return 0;
}


 类似资料:
  • 问题内容: 在Windows下如何控制Python中的鼠标光标,即将其移动到特定位置并单击? 问题答案: 在安装pywin32(在我的情况下为 )后,在WinXP上进行了Python 2.6(也已测试3.x)测试:

  • 问题内容: 我需要以某种方式捕获屏幕(作为打印屏幕),以便可以访问像素颜色数据,以进行一些图像识别,此后,我将需要在屏幕上生成鼠标事件,例如左键单击,拖放(移动鼠标)同时按下按钮,然后释放它)。完成后,图像将被删除。 注意:我需要捕获整个屏幕,用户可以看到所有内容,并且需要模拟程序窗口外的点击(如果有任何不同) 规格:Linux ubuntu语言:C ++ 性能不是很重要,“打印屏幕”功能将每隔1

  • 本文向大家介绍js控制鼠标事件移动及移出效果显示,包括了js控制鼠标事件移动及移出效果显示的使用技巧和注意事项,需要的朋友参考一下 鼠标事件的移动及移出效果都可以使用js来自定义,下面有个示例,效果为当事件发生改变时背景颜色也随着改变,适合新手朋友

  • 问题内容: 如何在Python中控制鼠标和键盘? 这个想法与Java中的Robot()类相同。可以说:将鼠标从此处移至此处,单击此处,然后将其写在屏幕上。 对于Windows,有win32api,但我主要使用Linux。 对于Linux,有Xlib,但它也适用于键盘吗?(仅找到对鼠标的引用) 有跨平台的解决方案吗?(Linux,Windows甚至OS X都很棒。) 问题答案: 对于鼠标,我发现py

  • 因此,我基本上是从以下教程学习OpenGL和GLFW库:http://www.opengl-tutorial.org/beginners-tutorials/tutorial-6-keyboard-and-mouse/ 在主应用程序循环中: 但出于某种原因,这种解决方案行不通。有什么方法可以用GLFW解决这个问题吗?

  • 任务是将物理光标移动到元素。 尝试以下操作: 和以下内容: