Easiest way to catch all keyboard input

Hi all,

My situation is that I want to be able to catch all the keyboard input when the application has the focus. This is so people can just enter say '55', and press enter and it will go to number 55. The main window just has a whole bunch of nested panels on it which are sized with wx.LayoutConstraints.

Basically, can someone tell me the simplest way to be able to catch all the keyboard input for all of these panels no matter which one has the focus?

Ive tried adding the following to the main frame constructor:

    self.Bind(wx.EVT_CHAR, self.testInput)

But it doesn't seem to work all the time, it seems rather intermittent. Any help would be great.

Cheers,
Jonathan

Jonathan Viney wrote:

Hi all,

My situation is that I want to be able to catch all the keyboard input when the application has the focus. This is so people can just enter say '55', and press enter and it will go to number 55. The main window just has a whole bunch of nested panels on it which are sized with wx.LayoutConstraints.

Basically, can someone tell me the simplest way to be able to catch all the keyboard input for all of these panels no matter which one has the focus?

The straightforward way would be to add an event handler for each of those panels and point them to the same function. You may also want to try wxWANTS_CHARS in your main window, although the docs aren't clear if it catches key events of children, too.

A more hackish approach may be to derive a class from wxPanel and add an event handler to that, so you only need to write the event handler once, and can use your new wxMyPanel wherever. Or so I think...

The straightforward way would be to add an event handler for each of those panels and point them to the same function. You may also want to try wxWANTS_CHARS in your main window, although the docs aren't clear if it catches key events of children, too.

A more hackish approach may be to derive a class from wxPanel and add an event handler to that, so you only need to write the event handler once, and can use your new wxMyPanel wherever. Or so I think...

Thanks for that. The wx.WANTS_CHARS style didn't seem to make any difference but I think that is only if you want to catch all keystrokes such as SHIFT and ALT etc....

I had thought about the 2nd approach you mentioned, but thought I'd see if anyone else had something a little simpler first. I've implemented this solution and it works perfectly.

For future reference if anyone is interested, the code for the base panel was:

class MyPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)
        self.Bind(wx.EVT_CHAR, self.onKey)

    def onKey(self, event):
        wx.PostEvent(self.GetParent(), event)

This simply passes key events up through all the panels to be processed by the main frame.

Thanks for the help,
Jonathan