tags:

views:

162

answers:

1

Hi All

The following key event is not working. Any idea?

class Frame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, -1, title='testing', size=(300,380),                      style=                                                                                        wx.MINIMIZE_BOX|wx.SYSTEM_MENU
                                                                                            |wx.CAPTION|wx.CLOSE_BOX|wx.CLIP_CHILDREN)  

        self.tree = HyperTreeList(self, style = wx.TR_DEFAULT_STYLE |
                                                wx.TR_FULL_ROW_HIGHLIGHT | wx.TR_HAS_VARIABLE_ROW_HEIGHT | wx.TR_HIDE_ROOT)

        # create column
        self.tree.AddColumn("coll")

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

 def OnKeyDown(self, event):
        keycode = event.GetKeyCode()
        print "keycode ", keycode
        if keycode == wx.WXK_ESCAPE:
            print "closing"
            self.Close()

Regards,

+1  A: 

Problem here is that focus is taken by tree cntrl, and hence all keyevent go to it not the mainframe hence binding to mainframe isn't working. So the first instinct would be to bind to tree cntrl e.g.

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

but that will also not work as, tree itself consists of two child windows, a header window and a main window, seeing the code of HyperTreeList what will work is this

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

But that looks inelegant and relies on internal detail of HyperTreeList, and _main_win which is derived from CustomTreeCntrl itself uses that event, so you must also be careful with catching such events.

Anurag Uniyal