Question

Goal

I want to redirect the keyboard input to different windows depending on the key. Not all the key will be redirected to the same window, I can't use XSetInputFocus.


First Attempt : XGrabKey

I found that I can grab certain keys and then send an event to the right window. I used XGrabKey like this

XGrabKey( mDisplay(), 
                    XKeysymToKeycode(mDisplay(),XK_F1) , 
                    AnyModifier ,
                    RootWindow(mDisplay(), mScreenNum),
                    false, //Events aren't sent to the focused window
                    GrabModeAsync, 
                    GrabModeAsync );

It doesn't work for F1 to F4 keys, but it does for F5 and F6. It also doesn't work for some regular keys like e, r and t. There may be others. I need to be able to redirect the Fkeys, so this solution didn't work for me.


Second Attempt : XGrabKeyboard

I tried with XGrabKeyboard, like this

XGrabKeyboard(mDisplay(), 
                        RootWindow(mDisplay(), mScreenNum),
                        false, 
                        GrabModeAsync, 
                        GrabModeAsync, 
                        CurrentTime);

It works with all the Fkeys, but it created other problems. When the grab keyboard is active I can no longer move the windows with my mouse (it still works if the application move its window), I can't access to context menus and the blinking '|' isn't displayed in typing areas.

Other informations

This problem as already been talked about there, but they didn't find a solution.I am using Lubuntu(LXDE) and not GNOME and I still have the same problem.

The unmap event may give some informations:

  • When the keyboard is not grabbed and I right click

    the context menu is displayed, no unmap event.

  • When I exit the context menu :

    I have 3 unmap events : (I print the name of the window next to Unmap)

Unmap pcmanfm  
Unmap pcmanfm 
Unmap
  • When the keyboard is grabbed and I right click

    I have just 1 unmap event :

Unmap

It looks like the context menu is unmapped before it is displayed.


What I am asking for

I would like to either:

  • Use XGrabKeyboard without side effects
  • Use XGrabKey with any key
  • Use another fonction that would allow me to redirect keyboard input

Thank you.

Was it helpful?

Solution

I didn't find a solution for the XGrabKeyboard problem.

The XGrabKey failed for some keys because they were already grabbed by OpenBox. Openbox has some built-in hotkeys that use XGrabKey. Because I use AnyModifier, if any grab has been made that include this key then the grab will fail.

For example if a program grabs Ctrl+F1 you can't grab F1 with AnyModifier. You can grab F1 without modifier, but the grab will not be active if the user press Alt+F1 for example.

To correct this problem I removed the keyboard hotkeys of Openbox because they were not important to me. To do so I modified the configuration file.

The other solution is to grab the key with all the modifier but the one that is already grabbed. Grabbing the key without modifier isn't enough because for example Num-Lock is a modifier and you probably don't want your grab to be active or not depending on Num-Lock.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top