Search code examples
pythondialoggtkkeykey-bindings

How to get a gtkDialog's default response to trigger of the space bar as well


I have a messageDialog set up so that its default response is gtk.RESPONSE_OK so the okay button is clicked when the user hits enter even if the okay button does not have focus. I would like to also have the space bar trigget the default_response. What is the best way to do this?

This is with python 2.4 in a linux environment. Unfortunately I don't have permission to upgrade python.


Solution

  • Connect to the key-press-event signal on the message dialog:

    def on_dialog_key_press(dialog, event):
        if event.string == ' ':
            dialog.response(gtk.RESPONSE_OK)
            return True
        return False
    
    dialog = gtk.MessageDialog(message_format='Some message', buttons=gtk.BUTTONS_OK_CANCEL)
    dialog.add_events(gtk.gdk.KEY_PRESS_MASK)
    dialog.connect('key-press-event', on_dialog_key_press)
    dialog.run()
    

    Bear in mind, though, that changing users' expectations of the user interface is generally considered Not Cool.