Skip to content
Advertisement

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

Does anyone know an xlib function to trap a keypress event without losing the original focus? How to get rid of it?

(or “to use XGrabKey() without generating Grab-style focusout”?)

(or “How to get rid of NotifyGrab and NotifyUngrab focus events at system level?)

The XGrabKey will lose focus on key pressed and restore focus on key released.

And I want to trap the keypress without leak it to the original window (just as XGrabKey can do it).

References:

  1. …XGrabKey will steal focus… https://bugs.launchpad.net/gtkhotkey/+bug/390552/comments/8

  2. …The program receives control to do something in response to the key combination. Meanwhile, the program has been temporarily focused… During XGrabKey(board), discover which window had been focused

  3. …The XGrabKeyboard function actively grabs control of the keyboard and generates FocusIn and FocusOut events… http://www.x.org/archive/X11R6.8.0/doc/XGrabKeyboard.3.html#toc3

  4. …I can’t see a way to provide metacity’s current desktop changin behavior (changing and showing the popup dialog at the same time) without causing a Grab-type focus out on the window… https://mail.gnome.org/archives/wm-spec-list/2007-May/msg00000.html

  5. …Fullscreen mode should not exit on FocusOut events with NotifyGrab… https://bugzilla.mozilla.org/show_bug.cgi?id=578265

  6. grabbing keyboard doesnt allow changing focus … grabbing keyboard doesnt allow changing focus

  7. Focus Events Generated by Grabs (both the active grab of XGrabKeyboard and the passive grab of XGrabKey) http://www.x.org/releases/X11R7.6/doc/libX11/specs/libX11/libX11.html#Focus_Events_Generated_by_Grabs

  8. the XGrabKey source code: http://cgit.freedesktop.org/xorg/lib/libX11/tree/src/GrKey.c maybe we could modify this to get rid of focus-out events?

  9. there is “DoFocusEvents(keybd, oldWin, grab->window, NotifyGrab);” in ActivateKeyboardGrab(): http://cgit.freedesktop.org/xorg/xserver/tree/dix/events.c

I’m writting a one-keystroke to keys-combination(and mouse movement) mapping software:https://code.google.com/p/diyism-myboard/

I have realized it in Windows with RegisterHotKey() and UnRegisterHotKey(): https://code.google.com/p/diyism-myboard/downloads/detail?name=MyBoard.pas

And i want to migrate it into Linux with XGrabKey() and XUngrabKey(): https://code.google.com/p/diyism-myboard/downloads/detail?name=myboard.py

I have created $10 bounty to resolve this problem. We need more backers to place bounties. https://www.bountysource.com/issues/1072081-right-button-menu-flashes-while-jkli-keys-move-the-mouse-pointer

Advertisement

Answer

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

sudo apt-get build-dep xorg-server
apt-get source xorg-server
cd 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 devscripts
debuild -us -uc    #"-us -uc" to avoid the signature step
cd ..
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.0
apt-get source gtk+2.0
cd gtk+2.0-*
#modify or patch it: add "return TRUE;" in first line of popup_grab_on_window() of gtk/gtkmenu.c
dpkg-source --commit
debuild -us -uc  #"-us -uc" to avoid the signature step, maybe need: sudo apt-get install devscripts
cd ..
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

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement