[wxPython-users] app-global KeyEvents [was: FoldPanelBar and ScrolledPanel]

Robin Dunn robin at alldunn.com
Thu Sep 14 14:57:06 PDT 2006


Richard Harvey Chapman wrote:
> On a related note, how do I add a key event to this frame. I've tried 
> various combinations of the following, but the event handler, 
> OnKeyPressed, is never called.
> 
>             self.Bind(wx.EVT_KEY_DOWN, self.OnKeyPressed, self)
>             self.Bind(wx.EVT_CHAR, self.OnKeyPressed, self)
>             self.Bind(wx.EVT_KEY_DOWN, self.OnKeyPressed, self.canvas)
>             self.Bind(wx.EVT_CHAR, self.OnKeyPressed, self.canvas)
>             self.Bind(wx.EVT_KEY_DOWN, self.OnKeyPressed, panel)
>             self.Bind(wx.EVT_CHAR, self.OnKeyPressed, panel)
> 
>             # The frame itself
>             self.SetFocus()
>             # The figure canvas
>             self.canvas.SetFocus()
>             # the root-most panel that everything sits on.
>             panel.SetFocus()

Key and char events are only sent to the widget that currently has the 
keyboard focus, and Frames can't have it, and if a panel has focusable 
children then a panel won't have it either.  If you are wanting to have 
a way to respond to a particular key from anywhere in the app then using 
a menu accelerator is the way to do it.  You can either add "\tkeyname" 
to the text for a menu item and the accelerator will automatically be 
created, or you can use a wx.AcceleratorTable and attach it to your frame.


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





More information about the wxpython-users mailing list