I have an application that was originally working with wx2.8 and has since been ported to wx3.0. This application's frame has a "Connect" button on which SetDefault() gets called when the button is created. Under wx2.8, this button would generate a clicked event when the enter key was recognised. Under wx3.0, this no longer seems to function. I have been attempting to work around this using hot keys but they present their own set of problems with modal dialogues when the enter key is defined as a hot key. The hot key event apparently will not deliver the key event even if it was skipped. Does anyone have any suggestions or work arounds?
Last Friday, I thought that this might have something to do with initialising the frame with the wxWANTS_CHARS style and, indeed, this is the case. I have also found that the problem occurs if the button is not initialised with a wxID_OK ID. The following code demonstrates the issue:
#include "wx/wx.h"
class MyFrame: public wxFrame
{
private:
wxTextCtrl *text_status;
int click_count;
public:
MyFrame():
wxFrame(
0,
wxID_ANY,
"SetDefault",
wxDefaultPosition,
wxDefaultSize,
wxDEFAULT_FRAME_STYLE | wxCLIP_CHILDREN),
click_count(0)
{
wxPanel *panel(new wxPanel(this));
wxSizer *sizer(new wxBoxSizer(wxVERTICAL));
wxButton *button;
text_status = new wxTextCtrl(panel, wxID_ANY);
sizer->Add(text_status, 0, wxALL, 10);
button = new wxButton(panel, wxID_HIGHEST + 1, "Increment");
sizer->Add(button, 0, wxALL, 10);
button->SetDefault();
panel->SetSizerAndFit(sizer);
}
void on_increment(wxCommandEvent &event)
{
wxString content;
++click_count;
content.Printf("%d", click_count);
text_status->SetValue(content);
}
DECLARE_EVENT_TABLE()
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_BUTTON(wxID_OK, MyFrame::on_increment)
END_EVENT_TABLE()
class MyApp: public wxApp
{
virtual bool OnInit()
{
MyFrame *frame(new MyFrame);
frame->Show(true);
SetExitOnFrameDelete(true);
return true;
}
};
IMPLEMENT_APP(MyApp)
I found a work around of specifying an ID of wxID_OK to the default button.