X Keypress/Release Events Capturing Irrespective of Window in Focus

Why can't I capture these KeyPress / KeyRelease events from inside my loop?

Argument 3 of XGrabKeyboard is to quote the man page:

owner_events : Specifies a Boolean value that indicates whether the
keyboard events are to be reported as usual.

so should be True or False, not an event mask.

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

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

Detect Key Press on Windows Without Window Focus

If you want to detect 'key press' events occurred in other processes, you should implement Global Hook. You can define a callback function for keyboard input events using SetWindowsHookEx().

Note that the callback function must be in a DLL in order to make it Global Hook.

So your myprogram.exe should link a dll implementing the hook. Then myprogram.exe would be able to detect any keyboard events on Windows.

Following is a good example with an explanation.
http://www.codeproject.com/Articles/1264/KeyBoard-Hooks

XGrabKeyboard only blocks the keyboard while the program is running

Quoth the manual:

When the X server's connection to a client is closed either by an
explicit call to XCloseDisplay() or by a process that exits, the X
server performs the following automatic operations:

  • It disowns all selections owned by the client (see XSetSelectionOwner()).
  • It performs an XUngrabPointer() and XUngrabKeyboard() if the client has actively grabbed the pointer or the keyboard.
  • It performs an XUngrabServer() if the client has grabbed the server.
  • It releases all passive grabs made by the client.

Grabs are requested by clients and granted to clients. For the duration of a grab, all relevant events are delivered to the grabbing client. No client, no grab.

I don't see what's to fix here. If you want a piece of functionality working, make sure the application that implements it is running.

A function callback every time a key is pressed (regardless of which window has focus)?

Take a look at what others have done already. You can take a look on how this pykeylogger code handles Linux in its backend, and see if that works for you.



Related Topics



Leave a reply



Submit