您可以使用 XTest 功能。这个 X11 扩展包括模拟按键、鼠标按钮按下和鼠标移动的功能。
您可以在此处阅读手册页:http: //linux.die.net/man/3/xtestfakekeyevent
这个简短的 C 示例,当与-lX11
and链接时-lXtst
,应该将鼠标移动到屏幕的左上角并单击鼠标左键:
#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;
}