如何在 Linux 中控制鼠标移动?
我尝试在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”中没有写入任何内容
我该如何解决这个问题?
如果这不是控制鼠标的好方法,还有其他方法吗?
I try to control the mouse in Linux. Xlib seems to work, but when I try to use it with OpenCV, it keeps returning:
Resource temporarily unavailable
So I decided to write "/dev/psaux". The code is as follows:
#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
sudo ./my_psaux
and get
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"
How can I fix this?
If this is not a good method to control the mouse, is there another one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
太棒了感谢 R...
为了提醒我一些其他方法而不是
/dev/psaux
我尝试了
/dev/input/mouse*
和/dev/input/event*
>通过使用
我得到这个:
经过测试,只有
/dev/input/event10
有效。代码如下:Great thanks to R...
for reminding me of some other ways instead of
/dev/psaux
I tried
/dev/input/mouse*
and/dev/input/event*
By using
I get this:
After testing, only
/dev/input/event10
works. The code is as follows:鼠标不是环回/回显设备。它更像是一个终端。您是否希望将数据写入终端(将出现在屏幕上)以使相同的字符作为输入返回给您?这同样适用于鼠标;写入它的唯一一点是发送改变其模式的转义序列(例如使用的协议或分辨率)。
如果你想“控制”鼠标,你必须以其他方式注入事件,或者提供一个 fifo(命名管道)或伪 tty 来代替输入系统的
/dev/psaux
读自。然而,这可能是一种相当误导的做事方式...如果您解释为什么需要控制鼠标,也许我们可以为您提供更好的替代方法来完成您想要做的事情。
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...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.