一些键盘/鼠标事件无法捕获-xlib编程

发布于 2024-09-24 13:41:16 字数 2505 浏览 3 评论 0原文

我对这个程序有疑问。它列出了系统上运行的当前窗口及其窗口 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;                

    }
}

I have a problem with this program. It lists the current windows along with their window IDs, that are running on the system.

Result is that for a particular window ID that i entered, I got an odd result:
for applications like firefox or gedit, just the motion notify event works, non of the other events work.

For my terminal (bash), everything works: key press, key release, mouse notify, mouse release, ...

Why is this the case?

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;                

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文