C 使用Xlib獲取鼠標(biāo)單擊坐標(biāo)
c
C 使用Xlib獲取鼠標(biāo)單擊坐標(biāo),c,x11,xlib,C,X11,Xlib,我想知道如何在屏幕上的任何位置獲得Xlib鼠標(biāo)點(diǎn)擊的x和y坐標(biāo)。我找到了這篇文章,它得到了當(dāng)前的指針位置,但我不知道如何修改它,以便在單擊鼠標(biāo)時獲得x-y坐標(biāo)。我試著寫這段代碼,但它什么也沒做# # # # int main (){int x=-1,y=-1; event;
我想知道如何在屏幕上的任何位置獲得Xlib鼠標(biāo)點(diǎn)擊的x和y坐標(biāo)。我找到了這篇文章c語言實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊,它得到了當(dāng)前的指針位置
,
但我不知道如何修改它,以便在單擊鼠標(biāo)時獲得x-y坐標(biāo)。我試著寫這段代碼,但它什么也沒做
#include
#include
#include
#include
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;
}
如果有更簡單的方法在C中實(shí)現(xiàn)這一點(diǎn)c語言實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊,我很高興聽到它
如果有更簡單的方法在C中實(shí)現(xiàn)這一點(diǎn),我很高興聽到它
太好了!然后使用一些人已經(jīng)為您制作的
int x, y, scrn;
xdo_t *hndl = xdo_new(NULL);
xdo_mouselocation(hndl, &x, &y, &scrn);
xdo_free(hndl);
printf("Mouse coordinates: x = %4d, y = %4d\n", x, y);
您可能需要抓住指針
我不知道你是不是只想按一下發(fā)布的按鈕。我已將其改為印刷機(jī),但您可以使用以下兩種方式進(jìn)行選擇:
XSelectInput(display, root, ButtonPressMask|ButtonReleaseMask) ;
然后將按鈕釋放盒添加回
#include
#include
#include
#include
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;
}
#包括
#包括
#包括
#包括
int main(){
int x=-1,y=-1;
XEvent事件;
int按鈕;
Display*Display=XOpenDisplay(空);
如果(顯示==NULL){
fprintf(stderr,“無法連接到X服務(wù)器!\n”);
退出(退出失敗);
}
窗口根=XDefaultRootWindow(顯示);
XGrabPointer(顯示、根、假、按鈕提示、GrabModeAsync、,
GrabModeAsync、None、None、CurrentTime);
XSelectInput(顯示、根目錄、按鈕提示);
而(1){
XNextEvent(顯示和事件);
開關(guān)(事件類型){
外殼按鈕按下:
開關(guān)(event.xbutton.button){
案例按鈕1:
x=event.xbutton.x;
y=event.xbutton.y;
按鈕=按鈕1;
打破
案例按鈕3:
x=event.xbutton.x;
y=event.xbutton.y;
按鈕=按鈕3;
打破
違約:
打破
}
打破
違約:
打破
}
如果(x>=0&&y>=0)中斷;
}
如果(button==Button1)printf(“在%d%d\n,x,y處左鍵單擊”);
else printf(“右鍵單擊%d%d\n”,x,y);
XCloseDisplay(顯示);
返回0;
}
我知道這是幾年后的事了,所以這是給其他人的信息。首先,使用另一個庫不符合我對“更容易”的定義。我自己也在用xlib和C開發(fā)一些需要的東西,我想看看我能把代碼做得多么簡單
基本上,在現(xiàn)有程序的xlib中,您只需要四行代碼就可以完成這項(xiàng)工作,其余的都取決于您希望如何處理它,/、(*、*color)等
/**gcc位置.c-o位置-lX11**/#包括#包括int main(int argc,字符**argv){ ;//謝謝。該庫肯定比xlib本身更易于使用,非常適合我的項(xiàng)目。但我沒有看到任何函數(shù)在單擊鼠標(biāo)時返回坐標(biāo),因?yàn)槟氖纠谡{(diào)用時只返回坐標(biāo)。謝謝,代碼對我來說很好,但我必須更改為 我得到了與上述相同的錯誤。你不知道為什么?這兩個事件對我都有效。你使用的是哪個窗口管理器?我使用的是KDE窗口管理器。我做了一些研究,你的問題似乎是其他進(jìn)程已經(jīng)從根窗口捕獲了按鈕按下事件,我可以通過運(yùn)行2來重現(xiàn)這個問題你的程序?qū)嵗ǖ诙€給出了這個錯誤)。沒有額外的LIB,直截了當(dāng)?shù)卣f:未完成的答案(不管OPs請求的事件)。請不要從這個答案中刪除任何內(nèi)容。
#include
#include
#include
#include
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;
}
/** gcc position.c -o position -lX11 **/
#include
#include
int main (int argc, char **argv) {
Window root_window; //<--one
int root_x, root_y; //<--two
unsigned int mask; //<--three
Display *display = XOpenDisplay(NULL);
/*
Bool XQueryPointer(display, w, root_return, child_return, root_x_return, root_y_return,
win_x_return, win_y_return, mask_return)
Display *display;
Window w;
Window *root_return, *child_return;
int *root_x_return, *root_y_return;
int *win_x_return, *win_y_return;
unsigned int *mask_return;
*/
XQueryPointer(display, DefaultRootWindow(display), &root_window, &root_window, &root_x, &root_y, &root_x, &root_y, &mask); //<--four
printf("Mouse coordinates (X: %d, Y: %d)\n", root_x, root_y);
XCloseDisplay(display);
return 0;
}