/*The simulator of keyboard, for the test of input method.
*Copyright (c)2008-2009 Zhang Shunchang
*
*How to compile:
*gcc -I/usr/X11R6/include -L/usr/X11R6/lib -o autokbd autokbd.c -lX11 -lXtst
*How to run:
*$./autokbd input_file
*/
#include <X11/extensions/XTest.h>
#include <X11/keysym.h>
#include <X11/Xlib.h>
#include <stdio.h>
int key_stroke(int iKey, Display *pDisplay)
{
KeyCode keycode = XKeysymToKeycode(pDisplay,iKey);
if(((iKey>= 'a')&&(iKey <= 'z'))||(iKey == '\''))
{
XTestFakeKeyEvent( pDisplay, keycode, True, CurrentTime );
XTestFakeKeyEvent( pDisplay, keycode, False, CurrentTime );
return 0;
}else if(iKey == '\n')
{
XTestFakeKeyEvent( pDisplay, XKeysymToKeycode(pDisplay,XK_KP_Enter), True, CurrentTime );
XTestFakeKeyEvent( pDisplay, XKeysymToKeycode(pDisplay,XK_KP_Enter), False, CurrentTime );
XTestFakeKeyEvent( pDisplay, XKeysymToKeycode(pDisplay,XK_Return), True, CurrentTime );
XTestFakeKeyEvent( pDisplay, XKeysymToKeycode(pDisplay,XK_Return), False, CurrentTime );
sleep(1);
return 0;
}else
{
printf("ERROR:illegal characters stroke!\n");
return 1;
}
}
int main(int argc, char **argv)
{
Display* pDisplay = XOpenDisplay( NULL );
if(NULL == pDisplay) return 1;
if (argc != 2)
{
printf("Usage: autokbd input_file\n");
return 1;
}
FILE *fp = fopen(argv[1],"r+");
if(NULL == fp)
{
printf("cannot open the input-file.\n");
return 1;
}
sleep(3);
char ch;
while(!feof(fp))
{
ch = fgetc(fp);
if(EOF == ch) break;
key_stroke(ch,pDisplay);
}
XCloseDisplay(pDisplay);
fclose(fp);
return 0;
}
|