[pygtk] Receive events without focus

Tim Evans t.evans.638 at gmail.com
Tue Jun 4 06:14:21 WST 2013

On 2013-05-31 20:44, Thomas Guettler wrote:
> Hi,
> I write a native GUI with gtk, which needs to receive keyboard events
> although the window does not have the focus.
> The gtk window should be minified and run in background. If the user
> types a shortcut, the window should get on the foreground and get the
> focus.
> Up to now the shortcut does not matter. It should be easy and fast to be
> entered and should not clash with a shortcut which is already in use.
> The GUI should run on linux and windows.
> How to implement this?
> I guess this needs to be implemented in a platform specific way. If you
> know the answer for one platform, let me know.
> I asked the same question here:
> http://stackoverflow.com/questions/16834326/receive-shortcuts-without-focus

On Windows there is the RegisterHotKey function. This is not available 
via GTK, so you'd have to use the win32 APIs directly. The hard part 
will be getting the WM_HOTKEY message. I think you'll need to create a 
window using win32all so you can handle the message. There are plenty of 
example of using RegisterHotKey on the net, you'll just need to convert 
them to Python using win32all or ctypes.


The other way to do it on Windows is to load your own DLL (written in C) 
into the process of each running application. I can't remember the API 
for doing that, but it did have some advantages in terms of what events 
you could then override.

You might look at how AutoHotkey (http://www.autohotkey.com/) does it, 
or consider just using AutoHotkey to send the right keys to your app.

Tim Evans

More information about the pygtk mailing list