700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > linux 鼠标点击 linux下使用xlib模拟鼠标移动和点击

linux 鼠标点击 linux下使用xlib模拟鼠标移动和点击

时间:2021-05-07 13:55:44

相关推荐

linux 鼠标点击 linux下使用xlib模拟鼠标移动和点击

/* ref: /~danny/Projects/xwarppointer/ */

#include

#include

//头文件

#include

#include

#include

#include

//全局变量

Display *display;

Window root;

//初始化

void init()

{

if ((display = XOpenDisplay(NULL)) == NULL) {

fprintf(stderr, "Cannot open local X-display.\n");

return;

}

root = DefaultRootWindow(display);

}

//得到坐标

void GetCursorPos(int &x,int &y)

{

int tmp;unsigned int tmp2;

Window fromroot, tmpwin;

XQueryPointer(display, root, &fromroot, &tmpwin, &x, &y, &tmp, &tmp, &tmp2);

}

//设置坐标

void SetCursorPos(int x,int y)

{

int tmp;

XWarpPointer(display, None, root, 0, 0, 0, 0, x, y);

XFlush(display);

}

//模拟点击

/* /questions/programming-9/simulating-a-mouse-click-594576/ */

void mouseClick(int button)

{

Display *display = XOpenDisplay(NULL);

XEvent event;

if(display == NULL)

{

printf("Errore nell'apertura del Display !!!\n");

return;

}

memset(&event, 0x00, sizeof(event));

event.type = ButtonPress;

event.xbutton.button = button;

event.xbutton.same_screen = True;

XQueryPointer(display, RootWindow(display, DefaultScreen(display)), &event.xbutton.root, &event.xbutton.window, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);

event.xbutton.subwindow = event.xbutton.window;

while(event.xbutton.subwindow)

{

event.xbutton.window = event.xbutton.subwindow;

XQueryPointer(display, event.xbutton.window, &event.xbutton.root, &event.xbutton.subwindow, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);

}

if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) printf("Errore nell'invio dell'evento !!!\n");

XFlush(display);

usleep(100000);

event.type = ButtonRelease;

event.xbutton.state = 0x100;

if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) printf("Errore nell'invio dell'evento !!!\n");

XFlush(display);

XCloseDisplay(display);

}

int main()

{

init();

int x,y;

GetCursorPos(x,y);

printf("%d %d\n",x,y);

SetCursorPos(0,0);

XCloseDisplay(display);

mouseClick(Button1);

return 0;

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。