I have a MyFrame
which derives from wxFrame
. A wxTextCtrl
is added to this frame. Can I handle EVT_KEY_DOWN
of this text control in frame? Something like,
BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
EVT_KEY_DOWN(MyFrame::OnKeyDown)
END_EVENT_TABLE()
The above code seems to be not working. Documentation says events like this can only be handled by the object where the event is originated. So should I subclass wxTextCtrl
to handle this and somehow send the information to frame?
What is the best way of doing this?
The wxCommandEvent
and wxNotifyEvent
type events from a child controls are set to propagate upward to the parent frame automatically. However, the wxKeyEvent
is derived from the wxEvent
so it is not propagating to the parent frame. Well, you may use dynamic event handlers to route some of the events to any of wxEvtHandler
derived objects.
Under wxWidgets 2.8 you should use wxEvtHandler::Connect
. This method is described here. You may also look at this sample code.
Under wxWidgets 2.9 and SVN you should use wxEvtHandler::Bind<>
:
MyFrame::MyFrame(...)
{
m_textcontrol->Bind(wxEVT_KEY_DOWN, &MyFrame::OnTextControlKeyDown, this);
}
The wxEvtHandler::Bind<>
method is described here.