linux上是否有任何通用接口可以模拟鼠标的移动和单击? [英] Is there any general interfaces on linux to simulate mouse movements and click?

查看:311
本文介绍了linux上是否有任何通用接口可以模拟鼠标的移动和单击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经参考了很多教程,这些教程通过将这些事件映射到您自己的真实鼠标来控制鼠标光标的移动和单击,但是我注意到,每次必须将此鼠标与/dev/input中的一个指定事件关联时, /,如果我没有连接一只真正的鼠标,或者如果linux在该真正的鼠标上没有提供正确的事件编号,则该程序肯定会失败.

I have referred to a lot of tutorials on how to control your mouse cursor's movements and clicks by map these events to your own real mouse, but I noticed that everytime I must associate this mouse to one specified event in /dev/input/, if I didn't connect one real mouse, or if the linux didn't give the right event number on this real mouse, the program will definitely fail.

现在我需要编写一个通用程序,该程序可以在linux上制作一个完整的虚拟鼠标,也就是说,即使该程序实际上没有鼠标,该程序也可以应用于任何计算机,但是光标会做出反应只要我给他们提供距离,鼠标光标的移动方向,单击所花费的时间以及该单击持续的时间.

Now I need to write a generalized program which can make one complete virtual mouse on linux, that is to say, this program can be applied to any machine even the machine don't actually have one mouse, but the cursor will react as long as I give them the distance, the direction the mouse cursor move, the click it takes and how long this click lasts.

所以我只是想知道是否有不需要我必须映射到真正鼠标设备的常规接口,我试图访问/dev/input/mice事件,但是看来我只能获取光标的位置,并且只要点击信息移动或点击,有人可以告诉我更通用的界面吗?预先感谢您!!

So I just wonder is there any general interface which don't require a real mouse device I must map to, I have tried to access /dev/input/mice event, but it seems that I can only get cursor's location and click information as long as it moves or clicks, could anyone tell me a more general interface? Thank U in advance!!!

推荐答案

您可以使用XTest函数. 该X11扩展程序包括伪造按键,鼠标按键和鼠标移动的功能.

You could use the XTest functions. This X11 extension includes functions to fake key presses, mouse button presses and mouse movement.

您可以在此处阅读手册页: http://linux.die.net/man/3/xtestfakekeyevent

You can read the man page here: http://linux.die.net/man/3/xtestfakekeyevent

这个简短的C示例,当与-lX11-lXtst链接时,应将鼠标移至屏幕的左上角,然后单击鼠标左键:

This short C example, when linked with -lX11 and -lXtst, should move the mouse to the upper left corner of the screen and click the left mouse button:

#include <X11/Xlib.h>
#include <X11/extensions/XTest.h>

void move_mouse(Display* display, int x, int y){
    XTestFakeRelativeMotionEvent(display, x, y, 0);
    XFlush(display);
}
void set_mouse(Display* display, int x, int y){
    XTestFakeMotionEvent(display, 0, x, y, 0);
    XFlush(display);
}
void button_make(Display* display, unsigned int button){
    XTestFakeButtonEvent(display, button, True, 0);
    XFlush(display);
}
void button_break(Display* display, unsigned int button){
    XTestFakeButtonEvent(display, button, False, 0);
    XFlush(display);
}
int main(int argc, char **argv){
    Display *display = XOpenDisplay(NULL);
    set_mouse(display, 0, 0);
    button_make(display, 1);
    button_break(display, 1);
    return 0;
}

这篇关于linux上是否有任何通用接口可以模拟鼠标的移动和单击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆