2013-04-20 2 views
2

Xlib을 사용하여 마우스 커서로 x와 y 좌표를 얻는 방법을 알고 싶습니다. 나는 현재 포인터 위치Xlib로 마우스 커서 좌표 맞추기

How can I get the current mouse (pointer) position co-ordinates in X를 얻을이 게시물,

를 발견했습니다하지만 난 그것을 마우스를 클릭 할 때 x 및 y 좌표를 얻을 수 있도록 수정하는 방법을 모르겠어요. 이 코드를 작성하려고 시도했지만 아무 것도 수행하지 않습니다.

#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; 
} 

이벤트가 다른 창으로 전송되어 작동하지 않을 수 있습니다.

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에서이 작업을 수행하는 간단한 방법이 있다면, 나는 그것을 듣고 행복 : 또 다른 문제는 내가 XSelectInput 기능에 ButtonPressMask를 추가 할 때 다음과 같은 오류를 얻을 수 있다는 것입니다.

답변

2

아마도 포인터를 가져와야 할 것입니다.

릴리스의 버튼 누르기 만 원하는지 여부는 알 수 없습니다. 나는 프레스로 변경했습니다,하지만 당신은 모두 선택할 수 있습니다.

XSelectInput(display, root, ButtonPressMask|ButtonReleaseMask) ; 

과에 다시 ButtonRelease 케이스를 추가

#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; 
} 
+0

고마워, 코드가 잘 작동하지만 ButtonRelease로 변경해야한다. 그렇지 않으면 위에서 설명한 것과 같은 오류가 발생한다. 왜 그랬는지 모를거야? – rex123

+0

두 가지 사건 모두 저에게 효과적이었습니다. 어떤 창 관리자를 사용하고 있습니까? KDE 창 관리자를 사용하고 있습니다. – parkydr

+0

화합을 사용하고 있습니다. – rex123

1

If there is simpler way to do this in C, I am happy to hear it.

Perfect! 그런 다음 일부 사람들이 이미 당신을 위해 만든 libxdo library을 사용하십시오.

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); 
+0

감사합니다. 라이브러리는 확실히 xlib보다 훨씬 사용하기 쉽고 내 프로젝트에 완벽합니다. 하지만 마우스를 클릭 할 때 좌표를 반환하는 함수를 보지 못했습니다. 예제에서는 호출 할 때 좌표 만 반환하기 때문입니다. – rex123

1

나는이 밖으로 그래서 이것은 정보입니다 몇 년 알고 다른 사람들이 따라 오는 것. 첫째, 아직 또 다른 라이브러리를 사용하는 것이 내 정의에 더 적합하지 않습니다. xlib와 C 만 사용하여이 작업이 필요한 작업을하고 있었고 코드를 얼마나 간단하게 만들 수 있는지보고 싶었습니다.

기본적으로 기존 프로그램의 xlib에서이 작업을 수행하는 데 필요한 코드는 네 줄뿐입니다. 나머지는 printf/sprintf, grab_pixel_color (Display * display, XColor * color) 등입니다.

/** gcc position.c -o position -lX11 **/ 
#include <X11/Xlib.h> 
#include <stdio.h> 

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; 
} 

다른 사람들에게 유용하기를 바랍니다.