Return to Snippet

Revision: 17633
at September 11, 2009 01:38 by wearetherock


Initial Code
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>

#define PRESS_UP	0
#define PRESS_DOWN	1
#define PRESS_LEFT	2
#define PRESS_RIGHT	3
#define PRESS_ENTER	4
#define PRESS_ESC	5
#define PRESS_HOME	6

 Display *display;
 unsigned int keycode;

 int press(int key){
	display = XOpenDisplay(NULL);

	if(key == PRESS_UP)
		keycode = XKeysymToKeycode(display, XK_Up);
	else if(key == PRESS_DOWN)
		keycode = XKeysymToKeycode(display, XK_Down);
	else if(key == PRESS_LEFT)
		keycode = XKeysymToKeycode(display, XK_Left);
	else if(key == PRESS_RIGHT)
		keycode = XKeysymToKeycode(display, XK_Right);
	else if(key == PRESS_ENTER)
		keycode = XKeysymToKeycode(display, XK_Return);
	else if(key == PRESS_ESC)
		keycode = XKeysymToKeycode(display, XK_Escape);
	else if(key == PRESS_HOME)
		keycode = XKeysymToKeycode(display, XK_Home);

	XTestFakeKeyEvent(display, keycode, True, 0);
	XTestFakeKeyEvent(display, keycode, False, 0);
	XFlush(display);
	return 0;
}

int main(int argc, char *argv[]){
	press(PRESS_UP);
}

Initial URL

                                

Initial Description
<pre>
To compile
$ gcc -lXtst -lX11 Util_KeyPressSimulator.c

To create shared library
$ gcc -lXtst -lX11 -c Util_KeyPressSimulator.c -o Util_KeyPressSimulator.o 
$ gcc -lXtst -lX11 -shared -o Util_KeyPressSimulator.so Util_KeyPressSimulator.o
</pre>

Initial Title
Simulate Key Press

Initial Tags
c

Initial Language
C