我正在使用 xlib,并且我有类似的东西来检查窗口/子窗口的事件:
// Dispatch X11 events in a more friendly format
static inline bool xwin_event(xwin_t *xwin, event_t *evt) {
XEvent event;
if (!XCheckWindowEvent(xwin->xconn->dpy, xwin->window, 0xFFFFFFFF, &event)) {
return false;
}
if (event.type == ClientMessage) {
printf("Got event, wid: %i\n", event.xany.window);
}
}
我循环调用它。 我正在这样建造我的窗户:
// Define events we want
XSelectInput(xconn->dpy, xwin->window,
KeyPressMask |
ButtonPressMask | ButtonReleaseMask |
EnterWindowMask | LeaveWindowMask |
PointerMotionMask | ExposureMask |
StructureNotifyMask | SubstructureNotifyMask);
// Grab some window manager events
xwin->proto = XInternAtom(xconn->dpy, "WM_PROTOCOLS", 1);
xwin->close = XInternAtom(xconn->dpy, "WM_DELETE_WINDOW", 0);
XSetWMProtocols(xconn->dpy, xwin->window, &xwin->close, 1);
由于某种原因,我从未看到任何 ClientMessage 事件从队列中出来。 如果我检查类似的内容(这不允许我按窗口过滤):
if (!XPending(xwin->xconn->dpy)) {
return false;
}
XNextEvent(xwin->xconn->dpy, &event);
一切顺利。 这是一个已知问题吗?
是的,
XCheckWindowEvent
的手册页明确指出了这一点
XCheckWindowEvent() 无法返回 ClientMessage、MappingNotify、 SelectionClear、SelectionNotify 或 SelectionRequest 事件,因为 这些事件类型根据定义是不可屏蔽的。
我花了几个小时解决这个问题,终于解决了。我希望这个答案可以为某人节省一些时间;
由于
XCheckWindowEvent
不分发任何 ClientMessage
事件,我查看了还有哪些其他事件函数,我发现了 XCheckTypedWindowEvent
;
在此示例中,我正在处理一条
WM_DELETE_WINDOW
消息;
XEvent event;
while(XCheckTypedWindowEvent(display, m_Window, ClientMessage, &event)){
// Client messages go here
if (event.xclient.data.l[0] == m_WmDeleteMessage) {
// Handle Close Message here
}
}
while (XCheckWindowEvent(display, m_Window, ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask | FocusChangeMask | EnterWindowMask | LeaveWindowMask, &event)) {
switch (event.type) {
// Process all other events right here
}
}
这方面的文档确实不是很好......