I'm trying to capture keyboard events that happen inside a wx.Frame, and I would expect the following code to capture those events. However, the handler OnKeyDown is never called when I run the code:
import logging as log
import wx
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(200,100))
self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
self.Bind(wx.EVT_KEY_UP, self.OnKeyDown)
self.Bind(wx.EVT_CHAR, self.OnKeyDown)
self.SetFocus()
self.Show(True)
def OnKeyDown(self, event=None):
log.debug("OnKeyDown event %s" % (event))
if __name__ == "__main__":
app = wx.App(False)
gui = MainWindow(None, "test")
app.MainLoop()
If anyone knows how to do this, I would appreciate some help.
I figured out that I can add a panel to the frame, and a panel is much more receptive of keyboard events.
import wx
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(200,100))
self.panel = wx.Panel(self, wx.ID_ANY)
self.panel.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
self.panel.Bind(wx.EVT_KEY_UP, self.OnKeyDown)
self.panel.Bind(wx.EVT_CHAR, self.OnKeyDown)
self.panel.SetFocus()
self.Show(True)
def OnKeyDown(self, event=None):
print "Event!"
if __name__ == "__main__":
app = wx.App(False)
gui = MainWindow(None, "test")
app.MainLoop()