Linux X11 - Global Keyboard Hook

后端 未结 5 1087
萌比男神i
萌比男神i 2021-01-30 14:26

Is it possible (or how) to create a mechanism (in Linux X11, C++) that works like a global hook in windows (SetWindowsHookEx())?

I would like to be able to catch the key

5条回答
  •  长发绾君心
    2021-01-30 14:32

    no idea if this helps, but I just found this in some code:

    
    
        void XFakeKeypress(Display *display, int keysym)
        { 
           XKeyEvent event;                     
           Window current_focus_window;         
           int current_focus_revert;
    
           XGetInputFocus(/* display = */ display, /* focus_return = */ 
              ¤t_focus_window, /* revert_to_return = */ ¤t_focus_revert);
    
           event.type = /* (const) */ KeyPress;
           event.display = display;
           event.window = current_focus_window;
           event.root = DefaultRootWindow(/* display = */ display);
           event.subwindow = /* (const) */ None;
           event.time = 1000 * time(/* tloc = */ NULL);
           event.x = 0;
           event.y = 0;
           event.x_root = 0;
           event.y_root = 0;
           event.state = /* (const) */ ShiftMask;   
           event.keycode = XKeysymToKeycode(/* display = */ display, 
              /* keysym = */ keysym);
           event.same_screen = /* (const) */ True;  
    
           XSendEvent(/* display = */ display, /* w = (const) */ InputFocus,
              /* propagate = (const) */ True, /* event_mask = (const) */ 
              KeyPressMask, /* event_send = */ (XEvent *)(&event));
    
           event.type = /* (const) */ KeyRelease;   
           event.time = 1000 * time(/* tloc = */ NULL);
    
           XSendEvent(/* display = */ display, /* w = (const) */ InputFocus,
              /* propagate = (const) */ True, /* event_mask = (const) */ 
              KeyReleaseMask, /* event_send = */ (XEvent *)(&event));
        }
    
    

提交回复
热议问题