I'm trying to get all the key events that occur in X, even of new
windows that open up after my program has started. I've had a look at
xkey and have been able to query all the existing windows on screen.
The mask I use is: (KeyPressMask|KeyReleaseMask|SubstructureNotifyMas k)
- which gives me all the key events and all the window creation events.
Then in my while loop I look at the event type to decide if it's a key
event or a window event - this seems to work quite nicely. If it's a
key event, I pass it off to a JNI (Java Native Interface) function, if
it's a window event, I want to try to select key events from that
window in the future. The xlib library seems to indicate all I need to
do is do a XSelectInput call using the new window id (which I
understand to be in xcwe->window) - but on the next iteration(s) of the
loop, none of the keyboard events occurring in the new window show up.
Obviously I'm missing something... would be grateful if anyone could
shed some light on this.

Thanks in advance.

Display * disp = XOpenDisplay(hostname);

XNextEvent(disp, &xev);
XKeyEvent * kev = NULL;
XCreateWindowEvent * xcwe = NULL;
if(xev.type == KeyPress || xev.type == KeyRelease){
kev = (XKeyEvent *)&xev;}
}else if(xev.type == CreateNotify){
//must have been a window event
XCreateWindowEvent * xcwe = (XCreateWindowEvent *)&xev;
XSelectInput(disp, xcwe->window,
(KeyPressMask|KeyReleaseMask|SubstructureNotifyMas k));