问题
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.
回答1:
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)
回答2:
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
来源:https://stackoverflow.com/questions/2526589/forwarding-keypresses-in-gtk