[wxPython-users] Re: fixed key bindings in notebook

Robin Dunn robin at alldunn.com
Tue Mar 18 21:30:53 PDT 2008


Franz Steinhäusler wrote:
> On Tue, 18 Mar 2008 21:07:58 +0100, Franz Steinhäusler <franz.steinhaeusler at gmx.at> wrote:
> 
> So, I am not sure, if that is normal, but after trying and searching all code and others, that tinkered code even works for me! :)
> 
> in wx.Notebook derived class:
> 
> self.Bind(wx.EVT_NAVIGATION_KEY, self.OnNavigationKey)
> 
>     def OnNavigationKey(self, event):
>         
>         ignorekey = False
>         if wx.GetKeyState(wx.WXK_CONTROL):
>             if wx.GetKeyState(wx.WXK_PAGEUP) or wx.GetKeyState(wx.WXK_PAGEDOWN):
>                 ignorekey = True
>             if wx.GetKeyState(wx.WXK_TAB):
>                 ignorekey = True
>         if ignorekey:
>             keycode = 0
>             if wx.GetKeyState(wx.WXK_TAB):
>                 keycode = wx.WXK_TAB
>             if wx.GetKeyState(wx.WXK_PAGEUP):
>                 keycode = wx.WXK_PAGEUP
>             if wx.GetKeyState(wx.WXK_PAGEDOWN):
>                 keycode = wx.WXK_PAGEDOWN
>             evt = wx.KeyEvent()
>             evt.m_controlDown = wx.GetKeyState(wx.WXK_CONTROL)
>             evt.m_keyCode = keycode
>             evt.m_shiftDown = wx.GetKeyState(wx.WXK_SHIFT)
>             evt.SetEventType(wx.wxEVT_KEY_DOWN)
> 
>             self.grandparent.txtDocument.GetEventHandler().ProcessEvent(evt)
>         else:
>             event.Skip()
> 
> It seems to work!

For the record, this works because wxSTC is a generic control and so it 
is  driven all from wx events.  If you had tried doing the same with a 
native widget such as a wxTextCtrl it would not work because an 
unhandled key event sent this way does not get converted to a native 
platform event and forwarded on to the native widget as if the user had 
pressed the key.


-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!





More information about the wxpython-users mailing list