Programmatically Show/Hide App Window with Global Shortcut Key

application-developmentgtkpygishortcut-keys

I'm writing a Notational Velocity clone for Gnome/Ubuntu using Quickly (PyGI) and I want to allow users to set a global shortcut key that will toggle the visibility of the app window when the app is running (something like Tilda does for its terminal emulator). I.e., once the app is launched, a user could press F2 to hide the window, and then press F2 again to make it visible and bring it to the front. I don't want to use an external hotkey app or anything that requires out-of-app settings. How would I go about this?

There's a similar question here: How can I listen on global keypress event? but the main answer there is a cludgy non-programmatic solution. One commenter mentions "grabbing the keyboard with an X api call" but I'm not sure where to start with that.

Best Answer

The Keybinder library does exactly this. If you check pull requests there are requests in for examples using pygi, one of which is me for py3k.