title: Handle C-q with XLookupString
body: <p>This is a working example of X11 code that handles Ctrl-q event to quit application:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;X11/Xlib.h&gt;
  #include &lt;X11/Xutil.h&gt;
  #include &lt;X11/Xos.h&gt;
  
  void exitOnCondition(char cond, const char *msg, int exitCode, Display *dpy, Window *w, GC *gc) {
      if(cond) {
          printf("%s\n", msg);
          if(dpy &amp;&amp; gc) XFreeGC(dpy, *gc);
          if(dpy &amp;&amp; w) XDestroyWindow(dpy, *w);
          if(dpy) XCloseDisplay(dpy);
          exit(exitCode);
      }
  }
  
  int main(int argc, char **argv) {
        Display *dpy = XOpenDisplay(0);
        exitOnCondition(dpy == 0, "Error: XOpenDisplay failed", -1, dpy, 0, 0);
  
        int blackColor = BlackPixel(dpy, DefaultScreen(dpy));
        int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));
  
        Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 
                       200, 100, 0, blackColor, blackColor);
  
        //Tell X Server to send MapNotify events
        XSelectInput(dpy, w, StructureNotifyMask | KeyPressMask);
  
        //Make window appear
        XMapWindow(dpy, w);
  
        //Graphics Context
        GC gc = XCreateGC(dpy, w, 0, 0);
  
        //Set white color for drawing
        XSetForeground(dpy, gc, whiteColor);
  
        //Wait for the MapNotify event
        for(;;) {
          XEvent e;
          XNextEvent(dpy, &amp;e);
          if (e.type == MapNotify) {
            break;
          }
        }
  
        //Draw the line
        XDrawLine(dpy, w, gc, 10, 60, 180, 20);
  
        //Send the "DrawLine" request to the server
        XFlush(dpy);
  
        char text[255];
        XEvent e;
        KeySym key;
        int numKeys = 0;
        for(;;) {
            XNextEvent(dpy, &amp;e);
            if(e.type == KeyPress) {
                //With modifier XLookupString will return garbage(?) in text[0] and key as latin1
                if((numKeys = XLookupString(&amp;e.xkey, text, 255, &amp;key, 0))) {
                    printf("lookup returned:\n");
                    for(int i = 0; i &lt; numKeys; i++) {
                        printf("text[%d]=%x\n", i, text[i]);
                    }
  
                    if(e.xkey.state == ControlMask &amp;&amp; key == XK_q) {
                        exitOnCondition(1, "C-Q pressed", 0, dpy, &amp;w, &amp;gc);
                    }
                }
            }
        }
  
        XFreeGC(dpy, gc);
        XDestroyWindow(dpy,w);
        XCloseDisplay(dpy);   
  }
  </code></pre>
  
  <p>Will this code correctly handle Ctrl-q event on any system?</p>
  
  <p>Can I use e.xkey.state to check for Ctrl modifier after XLookupString for any keyboard layout even when Ctrl is rebound to CAPS Lock(or anything else)?</p>
  
  <p>Why does XLookupString return one symbol text[0]==0x11 for Ctrl-q event and not text[0]==CtrlModifierCode text[1]=='q'?</p>