Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
    
How about modifying gconf settings for compiz to switch focus to your app? I think user keybindings are stored here. Problem is that once your app is out of focus, I don't think it will receive the keypress events. Using compiz settings could circumvent that. –  Ian B. Jul 27 '12 at 19:28
    
@Ian B., I'm not sure that's something that I can build into an app programatically. I don't want to do this just for my own use, but to build it into a distributable application (as in Tilda, Qnotero, Guake, etc.) –  monotasker Aug 9 '12 at 13:09
1  
The Keybinder library does exactly this: github.com/engla/keybinder if you check pull requests there are requests in for examples using pygi. –  hbdgaf Feb 23 at 11:04
    
Thanks @hbdgaf. If you submit that as an answer I'll accept it, since it's the first answer I've received that really does what I'm asking. –  monotasker Feb 23 at 17:31
    
I'm just sorry it took this long. It's a lib I'm using at the moment. –  hbdgaf Feb 23 at 17:38

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer

I realize this is an external setting, not in app, but I thought I'd write how to do this since I couldn't find documentation online.

I needed to read some compiz settings recently for my program, so figuring out how to change them was fairly straightforward. You can programatically set compiz settings in python using python-compizconfig. I've played with it a little bit and you can set values like so:

import compizconfig
context=compizconfig.Context()
commandplugin=context.Plugins['commands']
c0=commandplugin.Screen['command0']
c0.Value='xeyes'
key0=commandplugin.Screen['run_command0_key']
key0.Value='<Control><Primary>g'
context.Write()   #Note that sometimes you have to pass False to get it to update settings

The above sets the commands plugin of compiz to run xeyes using control-g keycombination. You'd probably have to do a check to make sure that the plugin was running (mine was off in ccsm by default). Note that to get many of the settings you can use the keys() function to list them (i.e.) context.Plugins.keys()

Here's a link to code that helped me figure out how this works, since I can't find any documentation: http://bazaar.launchpad.net/~ubuntu-branches/ubuntu/precise/compizconfig-python/precise/view/head:/src/compizconfig.pyx

share|improve this answer
    
When you say this is "an external setting" it looks like an app could set this programmatically. It makes the app dependent on compiz for its hotkey, but this is the closest I've seen to what I'm looking for without getting into some low-level X hocus pocus that is beyond me right now. Thanks. –  monotasker Sep 26 '12 at 21:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.