在GTK中转发按键 [英] Forwarding keypresses in GTK

查看:152
本文介绍了在GTK中转发按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Gedit插件编写一些代码。我使用Python和接口(显然)是GTK。



所以,我遇到的问题很简单:我有一个搜索框(一个gtk。条目)和右下方我有一个结果框(一个gtk.TreeView)。在搜索框中键入内容后,您会看到一堆结果,我希望用户能够按上/下键选择一个,然后按Enter键选择它并完成。事情是,我似乎无法找到一种方法来将向上/向下按键转发给TreeView。目前我有这段代码:
$ b $ pre $ def $ __onSearchKeyPress(self,widget,event):
$

如果[gtk.keysyms.Up,gtk.keysyms.Down]中的event.keyval:
print按下或向下
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

我可以清楚地看到我收到了正确的事件,但我发送的事件被TreeView忽略。任何想法?



预先感谢人。

解决方案

这个问题的正确答案(我不知道如何转发按键),但有一个替代解决方案来解决您的问题。



直接操作TreeView光标/选择,例如:

  path,column = browser.get_cursor()
browser.set_cursor((path [0] + 1,))#向下


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.

解决方案

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

这篇关于在GTK中转发按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆