【问题标题】:How to control mouse movement in linux?如何在linux中控制鼠标移动?
【发布时间】:2011-12-02 06:54:01
【问题描述】:

我尝试在 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”中没有写入任何内容?

谁能帮帮我?

如果这不是控制鼠标的好方法,谁能告诉我另一个?

【问题讨论】:

  • 除非您期望鼠标物理移动,否则我认为写入 PS/2 端口不是正确的做法...
  • 感谢您的评论。我以前也是这么想的。然而,正如@R.. 所说,它更像是一个终端。换句话说,我们还没有接触到鼠标。
  • 我怀疑,你听起来还是非常困惑...

标签: c linux input mouse


【解决方案1】:

非常感谢@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;
}

【讨论】:

    【解决方案2】:

    鼠标不是回送/回显设备。它更像是一个终端。您是否希望将数据写入终端(将出现在屏幕上)以使相同的字符作为输入返回给您?这同样适用于鼠标;写信给它的唯一要点是发送改变其模式的转义序列(例如使用的协议或分辨率)。

    如果你想“控制”鼠标,你必须以其他方式注入事件,或者提供一个 fifo(命名管道)或伪 tty 来代替 /dev/psaux 供输入系统读取。然而,这可能是一种相当误导的做事方式......

    如果您解释了为什么需要控制鼠标,也许我们可以为您提供更好的替代方法来解决您正在尝试做的事情。

    【讨论】:

    • 感谢您的回答。管道听起来是个好主意。我正在尝试做一些事情(比如 kinect)来控制鼠标移动。 Xlib 已尝试但失败。有什么建议吗?
    • @R.. 与您的回答相反,如果 /dev/input/event* 设备符合设备支持的事件,它实际上会回显您写给它们的任何内容。对 /dev/input/event* 设备的 write() 调用会注入写入的事件,就好像事件来自设备本身一样:参见 lxr.free-electrons.com/source/drivers/input/evdev.c#L457
    • 有趣。我还没有验证这一点,但它似乎是特定于 evdev 的行为。当然psaux 设备节点的行为并非如此。它允许将控制设备设置(如 dpi)的命令写入鼠标。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多