问题
So I have a standard window created with xlib that handles events:
while (keep_running){
XNextEvent (display, &event);
printf("event\n");
}
Now it doesn't seem to be calling the expose event, so I'm not able to draw in the window. I can see by the print statement that there are some events being fired, and I'd like to know what events they are.
So basically my question is, how can I get the event name to print it?
I'm still learning C, so any help is appriciated!
回答1:
so I don't totally agree with their design decision, but it was probably made 30 years ago, so now isn't the time for monday morning quarterbacking...
the type is a crazy union:
typedef union _XEvent {
int type; /* must not be changed */
XAnyEvent xany;
XKeyEvent xkey;
XButtonEvent xbutton;
XMotionEvent xmotion;
XCrossingEvent xcrossing;
XFocusChangeEvent xfocus;
XExposeEvent xexpose;
XGraphicsExposeEvent xgraphicsexpose;
XNoExposeEvent xnoexpose;
XVisibilityEvent xvisibility;
XCreateWindowEvent xcreatewindow;
XDestroyWindowEvent xdestroywindow;
XUnmapEvent xunmap;
XMapEvent xmap;
XMapRequestEvent xmaprequest;
XReparentEvent xreparent;
XConfigureEvent xconfigure;
XGravityEvent xgravity;
XResizeRequestEvent xresizerequest;
XConfigureRequestEvent xconfigurerequest;
XCirculateEvent xcirculate;
XCirculateRequestEvent xcirculaterequest;
XPropertyEvent xproperty;
XSelectionClearEvent xselectionclear;
XSelectionRequestEvent xselectionrequest;
XSelectionEvent xselection;
XColormapEvent xcolormap;
XClientMessageEvent xclient;
XMappingEvent xmapping;
XErrorEvent xerror;
XKeymapEvent xkeymap;
long pad[24];
} Event;
so you must first use the type to determine which event is being used:
if(event.type == KeyPress)
{
printf("keypress\n");
// now you know the type you can use the specific fields from `XKeyEvent xkey`...
}
or you could just log the type
printf("event type = (%d)\n",event.type);
the union works because each of the other possible elements also has type as the first element, so they all line up on the same address...
来源:https://stackoverflow.com/questions/33590613/xlib-print-event-name