覆盖wxPython中的KeyDown事件

时间:2012-02-27 09:35:30

标签: python events wxpython keydown

我写了wxTreeCtrl,我只想处理65到90之间的键码(Ascii:A-Z)。 我不想自己处理任何其他密钥代码。在我处理了65到90之间的键之后,我如何能够中止我的事件并使用标准的“key down”-event来进行wxTreeCtrl?

班级定义:

class ConfigTreeCtrl(wx.TreeCtrl):

方法绑定:

self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown, self)

方法定义:

def OnKeyDown(self, event):         
    now = time.time()  
    keyCode = event.GetKeyCode()
    if (keyCode >= 65 and keyCode <= 90) or ( keyCode >= 48 and keyCode <= 57):
        if self.searchString == "":
            self.searchString = chr(event.GetKeyCode())
        elif (now - self.lastKeyDown) < 2:
            self.searchString += str(chr(event.GetKeyCode()))
        else:
            self.searchString = str(chr(event.GetKeyCode()))
    self.lastKeyDown = now
    self.GetItem(self.searchString, self.GetRootItem())

感谢您的帮助和最好的问候

托马斯

1 个答案:

答案 0 :(得分:1)

event.Skip()可以在基本wxWindows类或本机控件中进一步处理事件。所以在你的方法中应该引入类似的东西:

def OnKeyDown(self, event):         
    now = time.time()  
    keyCode = event.GetKeyCode()
    if (keyCode >= 65 and keyCode <= 90) or ( keyCode >= 48 and keyCode <= 57):
        if self.searchString == "":
            self.searchString = chr(event.GetKeyCode())
        elif (now - self.lastKeyDown) < 2:
            self.searchString += str(chr(event.GetKeyCode()))
        else:
            self.searchString = str(chr(event.GetKeyCode()))
    else:
        event.Skip()
        return
    self.lastKeyDown = now
    self.GetItem(self.searchString, self.GetRootItem())