分享

Simulate Key Press

 quasiceo 2014-06-29

Simulate Key Press


 / Published in: C
 

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
  1. #include <X11/Xlib.h>
  2. #include <X11/keysym.h>
  3. #include <X11/extensions/XTest.h>

  4. #define PRESS_UP 0
  5. #define PRESS_DOWN 1
  6. #define PRESS_LEFT 2
  7. #define PRESS_RIGHT 3
  8. #define PRESS_ENTER 4
  9. #define PRESS_ESC 5
  10. #define PRESS_HOME 6

  11. Display *display;
  12. unsigned int keycode;

  13. int press(int key){
  14. display = XOpenDisplay(NULL);

  15. if(key == PRESS_UP)
  16. keycode = XKeysymToKeycode(display, XK_Up);
  17. else if(key == PRESS_DOWN)
  18. keycode = XKeysymToKeycode(display, XK_Down);
  19. else if(key == PRESS_LEFT)
  20. keycode = XKeysymToKeycode(display, XK_Left);
  21. else if(key == PRESS_RIGHT)
  22. keycode = XKeysymToKeycode(display, XK_Right);
  23. else if(key == PRESS_ENTER)
  24. keycode = XKeysymToKeycode(display, XK_Return);
  25. else if(key == PRESS_ESC)
  26. keycode = XKeysymToKeycode(display, XK_Escape);
  27. else if(key == PRESS_HOME)
  28. keycode = XKeysymToKeycode(display, XK_Home);

  29. XTestFakeKeyEvent(display, keycode, True, 0);
  30. XTestFakeKeyEvent(display, keycode, False, 0);
  31. XFlush(display);
  32. return 0;
  33. }

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

Report this snippet   Tweet

Comments

RSS Icon Subscribe to comments
Posted By: wearetherock on September 11, 2009

To prevent "Maximum number of clients reachedSegmentation fault" move display line 17 to main.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约