Monday, August 23, 2010

How to simulate CAPS LOCK press in Xlib

#include "stdio.h"
#include "X11/Xlib.h"
#include "X11/keysym.h"
#include "time.h"

Display *display=NULL;
unsigned int keycode;

int main ()
{
display = XOpenDisplay(NULL);
keycode = XKeysymToKeycode(display, XK_Caps_Lock);

printf ("\npressed\n");
XTestFakeKeyEvent(display, keycode, True, CurrentTime);
XFlush(display);
printf ("\nreleased\n");
XTestFakeKeyEvent(display, keycode, False, CurrentTime);
XFlush(display);
sleep(3);

/* type something here */

printf ("\npressed\n");
XTestFakeKeyEvent(display, keycode, True, CurrentTime);
XFlush(display);
printf ("\nreleased\n");
XTestFakeKeyEvent(display, keycode, False, CurrentTime);
XFlush(display);
sleep(3);

return 0;
}


Replace " " -s with < > for header files.
and compile using
gcc main.c -lX11 -lXtst

No comments:

Post a Comment