1

我有这个程序的问题。它列出了系统上正在运行的当前窗口及其窗口 ID。

结果是,对于我输入的特定窗口 ID,我得到了一个奇怪的结果:对于像 firefox 或 gedit 这样的应用程序,只有运动通知事件有效,其他事件都无效。

对于我的终端(bash),一切正常:按键,按键释放,鼠标通知,鼠标释放,......

为什么会这样?

Display * dis;
Window w;
dis = XOpenDisplay (0);
w=XDefaultRootWindow(dis);
Atom a = XInternAtom(dis, "_NET_CLIENT_LIST" , 1);
Atom actualType;
int format;
unsigned long numItems, bytesAfter;
unsigned char *data =0;
int status = XGetWindowProperty(dis,
                                w,
                                a,
                                0L,
                                (~0L),
                                0,
                                AnyPropertyType,
                                &actualType,
                                &format,
                                &numItems,
                                &bytesAfter,
                                &data);

if (status >= Success && numItems)
{
    int *array = (int*) data;
    int k;
    for (k = 0; k < numItems; k++)
    {
         // get window Id:
         Window w = (Window) array[k];
         char* name = '\0';
         status = XFetchName(dis, w, &name);
         if (status >= Success)
         {
             XSelectInput (dis, wev, KeyPressMask |PointerMotionMask |ButtonReleaseMask);
             printf("Found: %u  %s\n", w, name);
         }
         XFree(name);
    }
    XFree(data);
}
XEvent event;
while (1) {
    XNextEvent(dis, &event);
    switch  (event.type) {

        //MOUSE LOCATION
        case MotionNotify:
            fprintf(stdout,"x: %d y:%d\n",event.xmotion.x,event.xmotion.y);
        break;    
        //--------------------------------

        //KEYBOARD KEY(it does not wok)        
        case KeyPress:
             fprintf(stdout,"key: %d \n",event.xkey.keycode);
        break;
        //--------------------------------

        //KEYBOARD KEY(it does not wok)        
        case KeyRelease:
             fprintf(stdout,"key: %d \n",event.xkey.keycode);
        break;
        //--------------------------------

        case ButtonRelease:
            //show which button is released
            if (event.xbutton.button==1)
                fprintf(stdout,"mouse left click");
            if (event.xbutton.button==2)
                fprintf(stdout,"middle left click");
            if (event.xbutton.button==3)
                fprintf(stdout,"mouse right click");
        break;                

    }
}
4

0 回答 0