title: XKeysymToKeycode, XTestFakeKeyEvent and mapping
body: <p>I have the following problem:</p>
  
  <ul>
  <li>I'm trying to send fake key events to the X server.</li>
  </ul>
  
  <p>To do this, I'm aware of two methods:</p>
  
  <ul>
  <li>XSendEvent - I tried this <a href="http://www.doctort.org/adam/nerd-notes/x11-fake-keypress-event.html" rel="nofollow">tutorial</a> with the XK_Z instead of XK_Down
  <ul>
  <li>Does not work with GTK3</li>
  </ul></li>
  <li>XTestFakeKeyEvent - See my code below</li>
  </ul>
  
  <p>My problem is, None of this method takes account of the keyboard mapping. I mean, when I select AZERTY mapping I have a "z" character as I expect, when I select the QWERTY mapping I get a "w" and with my beloved BÉPO mapping I get an "é".</p>
  
  <p>How can I get the same character independently of the keyboard mapping??</p>
  
  <p>I'm using Ubuntu 12.10 under Unity.</p>
  
  <p>Here, my code for XTestFakeKeyEvent:</p>
  
  <pre><code>#include &lt;X11/Xutil.h&gt;
  #include &lt;X11/keysym.h&gt;
  #include &lt;X11/extensions/XTest.h&gt;
  #include &lt;iostream&gt;
  // The key code to be sent.
  // A full list of available codes can be found in /usr/include/X11/keysymdef.h
  /* g++ -o XFakeKey tst.c -L/usr/X11R6/lib -lX11 -lXtst */
  
  char *text = "z";
  main()
  {
  // Obtain the X11 display.
    Display *display = XOpenDisplay(0);
    if(display == NULL)
      return -1;
  
    Window focusWindow;
    int revert;
    XGetInputFocus(display, &amp;focusWindow, &amp;revert);
    KeyCode code = XKeysymToKeycode(display, XStringToKeysym(text));
  
    XTestFakeKeyEvent(display, code, True, CurrentTime);
    XTestFakeKeyEvent(display, code, False, CurrentTime);
  
    XCloseDisplay(display);
    return 0;
  }
  </code></pre>