【问题标题】:how to control Linux mouse cursor movement in C如何在 C 中控制 Linux 鼠标光标移动
【发布时间】:2014-05-06 11:18:42
【问题描述】:

我正在尝试通过 android 应用程序制作一个用于在 linux 服务器上远程控制鼠标的应用程序。作为一个初学者,我遇到了很多问题。在用C编写linux服务器之前,我尝试根据代码检查我是否可以在Linux上控制和移动鼠标:

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

int main(){
struct input_event event, event_end;
int i=0;
int fd = -1;

fd = open("/dev/input/event4", O_RDWR | O_NONBLOCK);
if(fd<0){
perror("error");
}

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( i=0; i<5; i++){

write(fd, &event, sizeof(event));// Move the mouse
write(fd, &event_end, sizeof(event_end));// Show move

sleep(1);
}
close(fd);
return 0;
}

构建编译后执行.. 没有任何反应 光标从不移动..这会是某种 VirtualBox 设置问题吗?

当我物理移动鼠标时,sudo cat /dev/input/event4 会显示奇怪的符号字符串。 这意味着能够通过 event4 来控制鼠标光标的移动,不是吗? 我想知道为什么光标不会移动...有人可以帮忙吗?

此外,如果有人建议使用库和函数来控制鼠标,我会很高兴

非常感谢

【问题讨论】:

    标签: c linux mouse


    【解决方案1】:

    您不能只写入设备文件并期望驱动程序的行为就像实际硬件正在发送这些事件一样。事情远比这复杂得多。

    如果您只关心 X Windows 环境,那么您很幸运。您可以使用此函数将事件发送到窗口:

    http://tronche.com/gui/x/xlib/event-handling/XSendEvent.html

    如果您不知道哪个窗口应该接收您的事件,只需将它们发送到根窗口,它们就会被适当地路由。

    还有一个库。

    http://www.x.org/releases/X11R7.6/doc/libXtst/recordlib.html

    请记住,在 X windows 中,事件有一个标志,指示事件是来自实际硬件还是由上述方法之一合成。在大多数情况下,程序只是忽略此标志并且无论如何都表现相同。但有时你会遇到奇怪的惊喜。

    【讨论】:

    • 谢谢你的评论..我通过关闭鼠标集成模式解决了移动鼠标光标的问题..我会尝试使用你给我的 API 再次谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    • 2013-01-02
    • 2016-12-10
    相关资源
    最近更新 更多