Simulate Key Press


/ Published in: C
Save to your folder(s)

<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>


Copy this code and paste it in your HTML
  1. #include <X11/Xlib.h>
  2. #include <X11/keysym.h>
  3. #include <X11/extensions/XTest.h>
  4.  
  5. #define PRESS_UP 0
  6. #define PRESS_DOWN 1
  7. #define PRESS_LEFT 2
  8. #define PRESS_RIGHT 3
  9. #define PRESS_ENTER 4
  10. #define PRESS_ESC 5
  11. #define PRESS_HOME 6
  12.  
  13. Display *display;
  14. unsigned int keycode;
  15.  
  16. int press(int key){
  17. display = XOpenDisplay(NULL);
  18.  
  19. if(key == PRESS_UP)
  20. keycode = XKeysymToKeycode(display, XK_Up);
  21. else if(key == PRESS_DOWN)
  22. keycode = XKeysymToKeycode(display, XK_Down);
  23. else if(key == PRESS_LEFT)
  24. keycode = XKeysymToKeycode(display, XK_Left);
  25. else if(key == PRESS_RIGHT)
  26. keycode = XKeysymToKeycode(display, XK_Right);
  27. else if(key == PRESS_ENTER)
  28. keycode = XKeysymToKeycode(display, XK_Return);
  29. else if(key == PRESS_ESC)
  30. keycode = XKeysymToKeycode(display, XK_Escape);
  31. else if(key == PRESS_HOME)
  32. keycode = XKeysymToKeycode(display, XK_Home);
  33.  
  34. XTestFakeKeyEvent(display, keycode, True, 0);
  35. XTestFakeKeyEvent(display, keycode, False, 0);
  36. XFlush(display);
  37. return 0;
  38. }
  39.  
  40. int main(int argc, char *argv[]){
  41. press(PRESS_UP);
  42. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.