点击鼠标获得与Xlib的坐标 [英] getting mouseclick coordinates with Xlib
问题描述
我想知道如何获得一个鼠标点击的x和y坐标与Xlib的屏幕上的任何地方。我发现这个职位它获取当前指针位置
I would like to know how to get the x and y coordinates of a mouseclick with Xlib anywhere on the screen. I've found this post which gets the current pointer position
<一个href=\"http://stackoverflow.com/questions/3585871/how-can-i-get-the-current-mouse-pointer-position-co-ordinates-in-x\">How我可以得到当前鼠标(指针)的位置坐标x中,
但我不知道如何使其获得点击鼠标时的X,Y坐标修改。
我试着写code,但它什么都不做。
but I don't know how to modify it so it gets the x y coordinates when the mouse is clicked. I've tried to write this code but it does nothing.
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
int main (){
int x=-1,y=-1;
XEvent event;
int button;
Display *display = XOpenDisplay(NULL);
if (display == NULL) {
fprintf(stderr, "Cannot connect to X server!\n");
exit (EXIT_FAILURE);
}
Window root= XDefaultRootWindow(display);
XSelectInput(display, root, ButtonReleaseMask) ;
while(1){
XNextEvent(display,&event);
switch(event.type){
case ButtonRelease:
switch(event.xbutton.button){
case Button1:
x=event.xbutton.x;
y=event.xbutton.y;
button=Button1;
break;
case Button3:
x=event.xbutton.x;
y=event.xbutton.y;
button=Button3;
break;
default:
break;
}
break;
default:
break;
}
if(x>=0 && y>=0)break;
}
if(button==Button1)printf("leftclick at %d %d \n",x,y);
else printf("rightclick at %d %d \n",x,y);
XCloseDisplay(display);
return 0;
}
该事件可能发送到其他窗口,这是它不工作的原因。另一个问题是,当我添加按钮pressMask到XSelectInput功能我得到以下错误:
The events are probably send to other windows and that is the reason it's not working. Another problem is that when I add ButtonPressMask to the XSelectInput function I get the following error:
X Error of failed request: BadAccess (attempt to access private resource denied)
Major opcode of failed request: 2 (X_ChangeWindowAttributes)
Serial number of failed request: 7
Current serial number in output stream: 7
如果有在C做这个简单的方法,我很高兴听到这个消息。
If there is simpler way to do this in C, I am happy to hear it.
推荐答案
您可能会需要获取指针。
You'll probably need to grab the pointer.
我不知道,如果你想只是想释放按钮presses。我已经改成了presses,但你可以拿起都与
I don't know if you want just want button presses of releases. I've changed it to presses, but you can pick up both with:
XSelectInput(display, root, ButtonPressMask|ButtonReleaseMask) ;
和添加ButtonRelease情况回。
and add the ButtonRelease case back in.
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
int main (){
int x=-1,y=-1;
XEvent event;
int button;
Display *display = XOpenDisplay(NULL);
if (display == NULL) {
fprintf(stderr, "Cannot connect to X server!\n");
exit (EXIT_FAILURE);
}
Window root= XDefaultRootWindow(display);
XGrabPointer(display, root, False, ButtonPressMask, GrabModeAsync,
GrabModeAsync, None, None, CurrentTime);
XSelectInput(display, root, ButtonPressMask) ;
while(1){
XNextEvent(display,&event);
switch(event.type){
case ButtonPress:
switch(event.xbutton.button){
case Button1:
x=event.xbutton.x;
y=event.xbutton.y;
button=Button1;
break;
case Button3:
x=event.xbutton.x;
y=event.xbutton.y;
button=Button3;
break;
default:
break;
}
break;
default:
break;
}
if(x>=0 && y>=0)break;
}
if(button==Button1)printf("leftclick at %d %d \n",x,y);
else printf("rightclick at %d %d \n",x,y);
XCloseDisplay(display);
return 0;
}
这篇关于点击鼠标获得与Xlib的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!