Why XGrabKey generates extra focus-out and focus-in events? Why XGrabKey generates extra focus-out and focus-in events? linux linux

Why XGrabKey generates extra focus-out and focus-in events?


My current code(from http://diyism-myboard.googlecode.com/files/myboard.py):

disp=Display()screen=disp.screen()root=screen.rootdef grab_key(key, mod):    key_code=string_to_keycode(key)    #3rd: bool owner_events, 4th: pointer_mode, 5th: keyboard_mode, X.GrabModeSync, X.GrabModeAsync    root.grab_key(key_code, mod, 0, X.GrabModeAsync, X.GrabModeAsync)    root.grab_key(key_code, mod|X.LockMask, 0, X.GrabModeAsync, X.GrabModeAsync) #caps lock    root.grab_key(key_code, mod|X.Mod2Mask, 0, X.GrabModeAsync, X.GrabModeAsync) #num lock    root.grab_key(key_code, mod|X.LockMask|X.Mod2Mask, 0, X.GrabModeAsync, X.GrabModeAsync)def main():    grab_key('Shift_L', X.NONE)    grab_key('Shift_R', X.NONE)    while 1:          evt=root.display.next_event()          if evt.type in [X.KeyPress, X.KeyRelease]: #ignore X.MappingNotify(=34)             handle_event(evt)if __name__ == '__main__':   main()

When i press "shift" key, the focus lost, and when i release it, the focus come back.


I looked at global hotkeys back in the early 90s for Irix, ultrix and solaris, as it had been easy to do on my Acorn BBC computer. Eventually we decided on solving this in a non-portable way on a level below xlib with some proprietary code. Since our software installation needed as superuser priviliges anyway, we were able to insert the appropriate software hooks as daemons.

For Linux (nowadays) you should probably look for a software solution by processing the keyboard event on the os level. I would start with having a look here: http://code.google.com/p/logkeys/

A more generic solution would be to have a small PC board with USB in and USB out, which acts to the computer as a mouse and keyboard and translates the keyboard keys as necessary. But this would not be so flexible if you want to change the mapping often.


Finally, as you know linux means freedom, i modified xserver to get rid of grab-style focusout:

sudo apt-get build-dep xorg-serverapt-get source xorg-servercd xorg-server-*#modify or patch dix/events.c: comment off "DoFocusEvents(keybd, oldWin, grab->window, NotifyGrab);" in ActivateKeyboardGrab(), comment off "DoFocusEvents(keybd, grab->window, focusWin, NotifyUngrab);" in DeactivateKeyboardGrab()sudo apt-get install devscriptsdebuild -us -uc    #"-us -uc" to avoid the signature stepcd ..sudo dpkg --install xserver-xorg-core_*.deb#clear dependencies:sudo apt-mark auto $(apt-cache showsrc xorg-server | grep Build-Depends | perl -p -e 's/(?:[\[(].+?[\])]|Build-Depends:|,|\|)//g')sudo apt-get autoremove

And i also need to get rid of XGrabKeyboard in gtk context menu:

sudo apt-get build-dep gtk+2.0apt-get source gtk+2.0cd gtk+2.0-*#modify or patch it: add "return TRUE;" in first line of popup_grab_on_window() of gtk/gtkmenu.cdpkg-source --commitdebuild -us -uc  #"-us -uc" to avoid the signature step, maybe need: sudo apt-get install devscriptscd ..sudo dpkg --install libgtk2.0-0_*.deb#clear dependencies:sudo apt-mark auto $(apt-cache showsrc gtk+2.0 | grep Build-Depends | perl -p -e 's/(?:[\[(].+?[\])]|Build-Depends:|,|\|)//g')sudo apt-get autoremove

Now myboard.py works well.

If you are using ubuntu raring-updates edition, you could give a try to:

https://code.google.com/p/diyism-myboard/downloads/detail?name=xserver-xorg-core_1.13.3-0ubuntu6.2_i386.deb

and:

https://code.google.com/p/diyism-myboard/downloads/detail?name=libgtk2.0-0_2.24.17-0ubuntu2_i386.deb