Guest User

X11 input filtering example // vertexSymphony

a guest
Jan 23rd, 2013
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #include <X11/Xlib.h>
  6. #include <X11/keysym.h>
  7.  
  8.  
  9. void injectToCurrentWindow(Display* targetDisplay, Window rootWindow, KeySym keyToInject, int keyModifiers, int keyPress)
  10. {
  11.  
  12.  
  13.     Window      xFocused;
  14.     XKeyEvent   xInputEvent;
  15.     int         xRevert;
  16.  
  17.     XGetInputFocus(targetDisplay, &xFocused, &xRevert);
  18.  
  19.     xInputEvent.display     = targetDisplay;
  20.     xInputEvent.window      = xFocused;
  21.     xInputEvent.root        = rootWindow;
  22.     xInputEvent.subwindow   = None;
  23.     xInputEvent.time        = CurrentTime;
  24.     xInputEvent.x           = 1;
  25.     xInputEvent.y           = 1;
  26.     xInputEvent.x_root      = 1;
  27.     xInputEvent.y_root      = 1;
  28.     xInputEvent.same_screen = True;
  29.     xInputEvent.keycode     = XKeysymToKeycode(targetDisplay, keyToInject);
  30.     xInputEvent.state       = keyModifiers;
  31.  
  32.     /* Take key press or release accordingly and dispatch event */
  33.     xInputEvent.type = keyPress?KeyPress:KeyRelease;
  34.     XSendEvent(xInputEvent.display,xInputEvent.window, True, KeyPressMask, (XEvent*) &xInputEvent );
  35. }
  36.  
  37.  
  38. int main(void)
  39. {
  40.     Display*    xDisplay;
  41.     Window      xRoot;
  42.  
  43.     KeySym      xKeyPressed;
  44.     XEvent      xKeyEvent;
  45.  
  46.     int         runLoop = 1;
  47.  
  48.  
  49.     /* Just open default display and check for errors */
  50.     xDisplay = XOpenDisplay(0);
  51.  
  52.     if (!xDisplay){
  53.         fprintf(stderr,"Failed to open display ! \r\n");
  54.         exit(1);
  55.     }
  56.  
  57.     /* Get root window and do the actual grab ... this will fail of other app has a grab */
  58.     xRoot = DefaultRootWindow(xDisplay);
  59.     XGrabKeyboard(xDisplay, xRoot, False, GrabModeAsync, GrabModeAsync, CurrentTime);
  60.  
  61.  
  62.     while(runLoop){
  63.  
  64.         /* Peek events */
  65.         XNextEvent(xDisplay,&xKeyEvent);
  66.  
  67.         if (xKeyEvent.type == KeyPress || xKeyEvent.type == KeyRelease){
  68.  
  69.             /* Convert and operate with KeySym ... We only allow "a" to pass, "q" stops the software  */
  70.             xKeyPressed = XKeycodeToKeysym(xDisplay, xKeyEvent.xkey.keycode, 0);
  71.  
  72.             if (xKeyPressed == XK_a) injectToCurrentWindow(xDisplay, xRoot, XK_a, xKeyEvent.xkey.state, xKeyEvent.type == KeyPress?1:0  );
  73.             if (xKeyPressed == XK_q) runLoop = 0;
  74.  
  75.             printf("[%s] You shall not pass !!! \r\n",XKeysymToString(xKeyPressed));
  76.         }
  77.  
  78.  
  79.  
  80.     }
  81.  
  82.     XUngrabKeyboard(xDisplay,CurrentTime);
  83.     XCloseDisplay(xDisplay);
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment