In wxPython how do you bind a EVT_KEY_DOWN event to the whole window?

ril3y picture ril3y · Aug 25, 2010 · Viewed 9.5k times · Source

I can bind an event to a textctrl box np. The problem is I have to be clicked inside of the textctrl box to "catch" this event. I am hoping to be able to catch anytime someone presses the Arrow keys while the main window has focus.

NOT WORKING:

 wx.EVT_KEY_DOWN(self, self.OnKeyDown)

WORKING:

self.NudgeTxt = wx.TextCtrl(self.panel, size=(40,20), value=str(5))
wx.EVT_KEY_DOWN(self.NudgeTxt, self.OnKeyDown)

I am pretty sure I am missing something easy. However am a bit stuck.

Answer

volting picture volting · Aug 25, 2010

Instead try binding to wx.EVT_CHAR_HOOK

e.g..

self.Bind(wx.EVT_CHAR_HOOK, self.onKey)

  ...

def onKey(self, evt):
    if evt.GetKeyCode() == wx.WXK_DOWN:
        print "Down key pressed"
    else:
        evt.Skip()