打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Simulate Key Press

Simulate Key Press


 / Published in: C
 

To compile$ gcc -lXtst -lX11 Util_KeyPressSimulator.cTo 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

Subscribe to comments

Posted By: wearetherock on September 11, 2009

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
linux 下如何通过软件模拟按键[转
ar
Android键盘输出增加按键码
android edittext 点击回车会响应两次的解决方案
#ifdef的用法
1259【毕设课设】基于单片机8155语音播放波形显示设计
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服