Chinaunix首页 | 论坛 | 博客
  • 博客访问: 402006
  • 博文数量: 20
  • 博客积分: 5010
  • 博客等级: 大校
  • 技术积分: 1270
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-16 09:18
文章分类
文章存档

2011年(6)

2010年(2)

2009年(1)

2008年(11)

我的朋友

分类: C/C++

2008-05-21 10:35:50

Theses days, i am doing some research about the pinyin to hanzi converter. And the decoder is completed, using trigram and Good-Turing smoothing. I want to compare to another pinyin input method based on SLM in my OpenSuSE. So i should simulate the key stroke so as to do a test.
After a search in Google, i found some eligible method. At last, i choose the XTestFakeKeyEvent() in Xlib. The following is my automaton keyboard:

/*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;
}

阅读(1173) | 评论(0) | 转发(0) |
0

上一篇:Something of these days

下一篇:gpg文档[zt]

给主人留下些什么吧!~~