views:

311

answers:

2

I'm writing a bit of code for a Gedit plugin. I'm using Python and the interface (obviously) is GTK.

So, the issue I'm having is quite simple: I have a search box (a gtk.Entry) and right below I have a results box (a gtk.TreeView). Right after you type something in the search box you are presented a bunch of results, and I would like the user to be able to press the Up/Down keys to select one, Enter to choose it, and be done. Thing is, I can't seem to find a way to forward the Up/Down keypress to the TreeView. Currently I have this piece of code:

def __onSearchKeyPress(self, widget, event):
    """
    Forward up and down keys to the tree.
    """
    if event.keyval in [gtk.keysyms.Up, gtk.keysyms.Down]:
        print "pressed up or down"
        e = gtk.gdk.Event(gtk.gdk.KEY_PRESS)
        e.keyval = event.keyval
        e.window = self.browser.window
        e.send_event = True
        self.browser.emit("key-press-event", e)
        return True

I can clearly see I'm receiving the right kind of event, but the event I'm sending gets ignored by the TreeView. Any ideas?

Thanks in advance people.

A: 

Did you include the key-press-event in the list of events the widget is allowed to receive? You can do that by calling

browser.add_events(gtk.gdk.KEY_PRESS_MASK)
ptomato
I'll check that, although I assume that's the case, because I can browse the TreeView using the arrow keys.
dguaraglia
+1  A: 

Not a proper answer to the question (I don't know how to forward key presses), but there's an alternative solution to your problem.

Manipulate the TreeView cursor/selection directly, for example:

path, column = browser.get_cursor()
browser.set_cursor((path[0] + 1,)) # Down
Johannes Sasongko
Yes, maybe that's the best way to go. Instead of forwarding the keystroke, I'll just change the position on the list myself. Now I only need to find a way of getting the focus to stay on the Entry once you press Down.
dguaraglia
I think you can do that by [returning True](http://library.gnome.org/devel/gtk/unstable/GtkWidget.html#GtkWidget-key-press-event) from the key-press-event handler.
Johannes Sasongko