xlib - print event name

主宰稳场 提交于 2020-01-06 03:33:10

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!