Skip to content Skip to sidebar Skip to footer

Wxpython: Binding Wx.evt_char_hook Disables Textctrl Backspace

I have a wx.TextCtrl and I want to be able to type in it, but also detect key presses such as UP, DOWN, RETURN, ESC. So I binded wx.EVT_KEY_DOWN to recognize any key press, and wx.

Solution 1:

You should call event.Skip() at the end of the event handler to propagate it further. This works for me:

import wx

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.panel = wx.Panel(self)
        self.text = wx.TextCtrl(self.panel)
        self.text.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
        self.text.Bind(wx.EVT_KEY_UP, self.OnKeyUp)

        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.text, 1)

        self.panel.SetSizerAndFit(self.sizer)  
        self.Show()

    def OnKeyDown(self, e):      
        code = e.GetKeyCode()
        if code == wx.WXK_ESCAPE:
            print("Escape")
        if code == wx.WXK_UP:
            print("Up")
        if code == wx.WXK_DOWN:
            print("Down")
        e.Skip()

    def OnKeyUp(self, e):
        code = e.GetKeyCode()
        if code == wx.WXK_RETURN:
            print("Return")
        e.Skip()


app = wx.App(False)
win = MainWindow(None)
app.MainLoop()

Post a Comment for "Wxpython: Binding Wx.evt_char_hook Disables Textctrl Backspace"