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 am developing a native application for Ubuntu using quickly. How can I disable few key bindings such as prevent Ctrl + F4 from closing the window, and other key combinations?

share|improve this question
    
You mean do this work using code ? –  Saeed Zarinfam Sep 3 '12 at 17:24
    
Not necessarily, may be changing some file which has keybindings, or even code if possible. –  Juzer Ali Sep 3 '12 at 17:32

1 Answer 1

up vote 1 down vote accepted

This is how you'd modify the global keybindings managed by Compiz. However, this would only work for systems using compiz as the window manager. It requires the python-compizconfig module. BTW, the default for close window is ALT+F4, it's the general settings plugin.

Here's how you could do it programmatically:

import compizconfig
context=compizconfig.Context()
commandplugin=context.Plugins['core']
c0=commandplugin.Screen['close_window_key']
defaultkey=c0.Value  # store the oldkey so you can put it back at the end
c0.Value='' # Unset the key
context.Write()   #Note that sometimes you have to pass False to get it to update settings

Note that to get many of the settings you can use the keys() function to list them (i.e.) context.Plugins.keys() as they are mostly just modified dictionaries.

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

What might actually be better, is to capture the destroy or delete-event signal sent to the window and test to see if it actually originated with a keypress. I wrote some code and was able to test for the keypress, but even though I can prevent the Gtk.main loop from being terminated, I can't prevent the window from being destroyed. Maybe someone else has an idea about that.

Edit: Here's my code that works correctly, but is there a better way to test for the key combo in the quit_test function?

Edit2: Modified to test for modifier keys and F4 correctly, but this DOES NOT work?! It will work for if I make it test for Gdk.ModifierType.CONTROL_MASK, but MOD1 which should be the Alt key doesn't work.

#!/usr/bin/python

from gi.repository import Gtk
keypress=True
def quit_test(widget, data):
    global keypress
    print "destroying"

    if keypress:
        keypress=False
        return True
    else:
        Gtk.main_quit()
        widget.destroy()

def handle_keys(widget,event):
    global keypress
    modifiers=Gtk.accelerator_get_default_mod_mask() 
    for mod in dir(Gdk.ModifierType): #just printing what the modifier is
        if (event.state&modifiers) == getattr(Gdk.ModifierType,mod): print mod
    if event.keyval==Gdk.KEY_F4 and (event.state&modifiers)==Gdk.ModifierType.MOD1_MASK:
        keypress=True
    print event.keyval, keypress

win=Gtk.Window()
win.connect('delete-event', quit_test)
win.connect('key-press-event', handle_keys)
label=Gtk.Label("just some text")
win.add(label)
win.show_all()
Gtk.main()
share|improve this answer
1  
I do the same in an application where the user shouldn't be able to close the window when fullscreen. In the window delete-event handler, just do a return True when the keycombo is pressed, else Gtk.main_quit(). –  Timo Sep 4 '12 at 8:46
    
@Timo Yes I found out this method somewhere today only. You should post this as a separate answer. I think that is a better way of dealing with it. Also I will be grateful to you if you can provide an example. I am unable to parse the function keys. –  Juzer Ali Sep 4 '12 at 13:28
    
@Timo, Yes please post... I included my code above in an edit, but while the Gtk.quit is avoided, the window is still destroyed. I don't know what I did wrong. –  Ian B. Sep 4 '12 at 14:30
    
Actually, figured out what I did wrong... The above now works, but would like to know if there's a better way to test for the key combo, perhaps directly in the quit_test function –  Ian B. Sep 4 '12 at 14:36
    
Your code is a bit buggy, 65513L is the alt-key, not alt+f4. So when tapping the alt-key and then trying to close by using the window's close button, it will fail. I however haven't been able yet to come up with something better (also a bit lack of time). –  Timo Sep 4 '12 at 17:01

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.