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 toXCloseDisplay()
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()
andXUngrabKeyboard()
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
What Is a Reasonable Amount of Inotify Watches with Linux
Differencebetween Ld_Library_Path and -L at Link Time
How to Export Symbol from Linux Kernel Module in This Case
Is There a Linux Command to Determine the Window Ids Associated with a Given Process Id
Linux How to Copy But Not Overwrite
Linux Command (Like Cat) to Read a Specified Quantity of Characters
How to Extract the Contents of an Rpm
Why Doesn't Perf Report Cache Misses
Get Specific Line from Text File Using Just Shell Script
How to List Library Dependencies of a Non-Native Binary
Symbolic Link: Find All Files That Link to This File
How to Run an X Program from Outside the X Session (E.G. from the Console or Ssh)
Counting Number of Directories in a Specific Directory
Why Is Clock_Gettime So Erratic
How to Paste Multi-Line Bash Codes into Terminal and Run It All at Once
Does Socket Io Involve Disk Io
Why Ulimit Can't Limit Resident Memory Successfully and How
Chmod - Protect Users' File Being Accessed So Only Owner Can Access