X11 Fake Key Event Generation using XTest Extension

Last week, I got a chance to refresh my X11 knowledge, which I learned in the year of 2002 and 2003. The task is to generate the fake key events to the application, as if the key events are coming from keyboard. This is basically used to convert the non-standard device event to keyboard event. I wrote a program using the old XSendEvent() function and completed it with few issues, specifically, unable to generate the ALT+TAB.

This week, I found the XTest Extension, it provides a very simple APIs to generate many X events. So I modified the existing program to use XTest API, to generate all key press events. The only issue here is, XTest extension must be installed in the system. Otherwise it will fail. The old XSendEvent() is a part of core XLib and no dependencies.

 /* fakeKey.c */
#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
#include <X11/extensions/XTest.h>
#include <unistd.h>
/* Send Fake Key Event */
static void SendKey (Display * disp, KeySym keysym, KeySym modsym){
 KeyCode keycode = 0, modcode = 0;
 keycode = XKeysymToKeycode (disp, keysym);
 if (keycode == 0) return;
 XTestGrabControl (disp, True);
 /* Generate modkey press */
 if (modsym != 0) {
  modcode = XKeysymToKeycode(disp, modsym);
  XTestFakeKeyEvent (disp, modcode, True, 0);
 }
 /* Generate regular key press and release */
 XTestFakeKeyEvent (disp, keycode, True, 0);
 XTestFakeKeyEvent (disp, keycode, False, 0); 

 /* Generate modkey release */
 if (modsym != 0)
  XTestFakeKeyEvent (disp, modcode, False, 0);

 XSync (disp, False);
 XTestGrabControl (disp, False);
}

/* Main Function */
int main (){
 Display *disp = XOpenDisplay (NULL);
 sleep (5);
 /* Send ASCII A & B */
 SendKey (disp, XK_A, 0);
 SendKey (disp, XK_B, 0);
 /* Send ALT+Tab */
 sleep (3);
 SendKey (disp, XK_Tab, XK_Alt_L);
 sleep (3);
 SendKey (disp, XK_Tab, XK_Alt_L);
}

Build and Run it using following commands:
$ gcc fakeKey.c -o fakeKey -lX11 -lXtst -lXext
$ ./fakeKey

Look into /usr/include/X11/ keysymdef.h header file to know about key symbols of other keys.

8 thoughts on “X11 Fake Key Event Generation using XTest Extension

  1. Hi Bharathi Subramanian,I looking for example code, which will capture every key press and print the key symbol. The scenario I have is very simple, I have some set of keys which are not mapped exactly as stated in /usr/include/X11/ keysymdef.h. But i have to simulate/send a fake key for that event, So, I need what key symbol for that keypress event and will use your above code to simulate the keypress event. Any pointers will help us, Thanks in Advance,Raja

  2. OR try the "showkey" [http://bharathi.posterous.com/one-day-one-gnulinux-command-showkey]

  3. Hi Bharathi, Thanks for the sample program. Is it possible to generate mouse events such as scrolling using xtest? I need this in my project.

  4. Hi there I think this is just what I am looking for!! Although I know nothing about x11. What I am trying to do is create an android app that displays simple console controllers and sends key presses to the computer. the keys can then be defined in the emulators so what you have is basically a universal wireless controller. Would this be doable?Is there much key lag time? Any help would be amazing thanksDave

Leave a comment