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.
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
Hi Raja, Try the "xev" tool, which print all the input event details.
OR try the "showkey" [http://bharathi.posterous.com/one-day-one-gnulinux-command-showkey]
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.
Yes. The button number is 5. See the example in:http://bharathi.posterous.com/x11-fake-mouse-events-generation-using-xtest
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
Thanks for your code. Now I have a question. How can I deploy at the same time two "virtual keys" using Senkey???
i need an application to simulate touch events in X. can you help me with that?