I try to control the mouse in Linux. Xlib seems to works, but when I try to use it with OpenCV, it keeps returning:
我尝试在Linux中控制鼠标。Xlib似乎可以工作,但当我尝试将它与OpenCV一起使用时,它总是返回:
Resource temporarily unavailable
So I decide to write "/dev/psaux". The code is as following:
所以我决定写“/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;
}
Compile it with:
编译:
gcc my_psaux.c -o my_psaux -std=gnu99 -g
Run and get
运行并获得
$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
However the mouse doesn't move. Then I open a new terminal, type in "sudo cat /dev/psaux" and run "my_psaux". But I just cat nothing. Nothing is written into "/dev/psaux" ?
然而,老鼠却一动不动。然后打开一个新的终端,输入“sudo cat /dev/psaux”并运行“my_psaux”。但我什么也不猫。没有写进“/dev/psaux”?
Could anyone help me?
有人能帮助我吗?
If this is not a good method to control the mouse, could anyone tell me another one?
如果这不是一个好的控制鼠标的方法,谁能告诉我另一个?
2 个解决方案
#1
16
Great thanks to @R.. for reminding me of some other ways instead of /dev/psaux
大感谢@R . .谢谢你提醒我其他的方法而不是/dev/psaux
So I tried /dev/input/mouse*
and /dev/input/event*
所以我尝试了/dev/input/mouse*和/dev/input/event*
By using
通过使用
cat /proc/bus/input/devices
I get this:
我得到了这个:
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
After testing, only /dev/input/event10
works. The code is as following:
测试之后,只有/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
2
The mouse is not a loopback/echo device. It's more like a terminal. Would you expect writing data to a terminal (which would appear on the screen) to make the same characters come back to you as input? The same applies to the mouse; the only point in writing to it is to send escape sequences that change its mode (e.g. the protocol used or the resolution).
鼠标不是回环/回波装置。它更像是一个终端。您是否期望将数据写入终端(将出现在屏幕上)以使相同的字符返回给您作为输入?这同样适用于老鼠;写入它的唯一一点是发送转义序列来改变它的模式(例如使用的协议或解析)。
If you want to "control" the mouse, you have to inject events some other way, or provide a fifo (named pipe) or pseudo-tty in place of /dev/psaux
for the input system to read from. However this is probably a rather misguided way to do things...
如果您想要“控制”鼠标,您必须以其他方式注入事件,或者为输入系统提供fifo(命名管道)或伪tty来代替/dev/psaux。然而,这可能是一种相当误导人的做事方式……
If you explain why you need to control the mouse, perhaps we could offer you better alternative approaches to what you're trying to do.
如果您解释为什么需要控制鼠标,也许我们可以为您提供更好的替代方法。
#1
16
Great thanks to @R.. for reminding me of some other ways instead of /dev/psaux
大感谢@R . .谢谢你提醒我其他的方法而不是/dev/psaux
So I tried /dev/input/mouse*
and /dev/input/event*
所以我尝试了/dev/input/mouse*和/dev/input/event*
By using
通过使用
cat /proc/bus/input/devices
I get this:
我得到了这个:
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
After testing, only /dev/input/event10
works. The code is as following:
测试之后,只有/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
2
The mouse is not a loopback/echo device. It's more like a terminal. Would you expect writing data to a terminal (which would appear on the screen) to make the same characters come back to you as input? The same applies to the mouse; the only point in writing to it is to send escape sequences that change its mode (e.g. the protocol used or the resolution).
鼠标不是回环/回波装置。它更像是一个终端。您是否期望将数据写入终端(将出现在屏幕上)以使相同的字符返回给您作为输入?这同样适用于老鼠;写入它的唯一一点是发送转义序列来改变它的模式(例如使用的协议或解析)。
If you want to "control" the mouse, you have to inject events some other way, or provide a fifo (named pipe) or pseudo-tty in place of /dev/psaux
for the input system to read from. However this is probably a rather misguided way to do things...
如果您想要“控制”鼠标,您必须以其他方式注入事件,或者为输入系统提供fifo(命名管道)或伪tty来代替/dev/psaux。然而,这可能是一种相当误导人的做事方式……
If you explain why you need to control the mouse, perhaps we could offer you better alternative approaches to what you're trying to do.
如果您解释为什么需要控制鼠标,也许我们可以为您提供更好的替代方法。