Detecting a specific keypress from anywhere in an app

I’ve got this tool with a bunch of different widgets. I’d like to make it so that I can hit F3 and no matter which widget has the focus, this one specific widget will react. Detecting F3 everywhere seems to be a pain. As far as I can tell, I have to bind the key-down event separately for every widget. This results in a lot of repeated code. Is there a better way of handling this?

One problem (I think) with this method is that for the find and replace text controls I created a class called RegexControl. RegexControl extends wx.TextCtrl and binds the key press events to internal methods.

···

On Wed, Jun 18, 2008 at 3:50 PM, raffaello barbarossa.platz@gmail.com wrote:

In init:

    children = self.panel.GetChildren()
    for j in range(len(children)):
        children[j].Bind(wx.EVT_KEY_UP,  self.OnKeyPress)

then:

    def OnKeyPress(self, event):


        event.Skip()
        if event.GetKeyCode() == 342: # F3
           print "Key F3 pressed on  ", event.GetEventObject().GetName()

There are worse pains in haec lacrimarum valle :slight_smile: than six lines of code.

2008/6/18 Mike Driscoll mike@pythonlibrary.org:

Jesse Aldridge wrote:

I’ve got this tool with a bunch of different widgets. I’d like to make it so that I can hit F3 and no matter which widget has the focus, this one specific widget will react. Detecting F3 everywhere seems to be a pain. As far as I can tell, I have to bind the key-down event separately for every widget. This results in a lot of repeated code. Is there a better way of handling this?

We got a similar request a week or so ago about nabbing the ESC key. IIRC, the easiest way is to create your widgets and then create a list of said widgets. Then loop over the list and bind each widget to your event handler. My understanding is that the reason is that the widget that has focus will eat the key events unless you bind an event handler and do something else.

Maybe someone else will have a better idea though.


Mike Driscoll

Blog: http://blog.pythonlibrary.org

Python Extension Building Network: http://www.pythonlibrary.org


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users