Catching 'enter' in a combo box

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
CorsairX
Earned a small fee
Earned a small fee
Posts: 15
Joined: Tue Jan 17, 2006 5:30 pm
Contact:

Catching 'enter' in a combo box

Post by CorsairX »

Hi,
I have a feeling that I'm missing something obvious but I can't figure this out. I have a panel that has 3 text controls and 2 combo boxs and I want to catch 'enter' key from any of these controls. The text control works fine, but for the life of me I cannot catch 'enter' from the two combo boxs.

My desired behavior is this:
Tab key - navigate to next control
Enter key - Process the event

With my code as is; if I set wxTE_PROCESS_ENTER on the combo boxes when I tab through or press enter, they just beep. If I set no style (0); then they tab through correctly but, on enter, also just tab to the next control.

I'd be grateful for any help!

My code is as follows:

Code: Select all

//Header

#ifndef WX_PRECOMP
	#include <wx/wx.h>
	#include <wx/panel.h>
#else
	#include <wx/wxprec.h>
#endif

#include <wx/event.h>
#include <wx/combobox.h>
#include <wx/textctrl.h>
#include <wx/panel.h>
#include <wx/sizer.h>


class pnlTodoItem : public wxPanel
{
public:	   
	pnlTodoItem(todoItem* t_data, wxWindow *parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxTAB_TRAVERSAL, const wxString &name = wxT("Todo item"));

	virtual ~pnlTodoItem();
		
private:

	//Data
	todoItem* data;
	wxArrayString categoryArray;


	//Callbacks
	void OnEnter(wxCommandEvent& event );

	DECLARE_EVENT_TABLE();

	wxComboBox  *drpStatus;
	wxTextCtrl *inpTodo;
	wxComboBox *drpCategory;
	wxTextCtrl *inpDue;
	wxTextCtrl *inpCreated;
	wxBoxSizer *szRoot;
	
	enum
	{
		ID_INPCREATED = 1004,
		ID_INPDUE = 1005,
		ID_DRPCATEGORY = 1006,
		ID_INPTODO = 1007,
		ID_DRPSTATUS = 1008
	};
	
	void CreateGUIControls();
};

Code: Select all

// Implementation

#include "pnlTodoItem.h"
BEGIN_EVENT_TABLE(pnlTodoItem, wxPanel)
	//Combo
	EVT_TEXT_ENTER(ID_DRPSTATUS, pnlTodoItem::OnEnter)
	EVT_TEXT_ENTER(ID_DRPCATEGORY, pnlTodoItem::OnEnter)

	//Text controls
	EVT_TEXT_ENTER(ID_INPCREATED,	pnlTodoItem::OnEnter)
	EVT_TEXT_ENTER(ID_INPDUE,		pnlTodoItem::OnEnter)
	EVT_TEXT_ENTER(ID_INPTODO,		pnlTodoItem::OnEnter)

	/* 
	//Works fine but with EVT_TEXT_ENTER there seems to be some interaction happening
	EVT_COMBOBOX(ID_DRPCATEGORY,	pnlTodoItem::OnEnter)
	EVT_COMBOBOX(ID_DRPSTATUS,		pnlTodoItem::OnEnter)
	*/
END_EVENT_TABLE()

pnlTodoItem::pnlTodoItem(todoItem* t_data, wxWindow *parent, wxWindowID id, const wxPoint &position, const wxSize& size, long style, const wxString &name)
: wxPanel(parent, id, position, size, style, name)
{
	this->data = t_data;
	CreateGUIControls();
}


pnlTodoItem::~pnlTodoItem()
{
}
void pnlTodoItem::CreateGUIControls()
{
	szRoot = new wxBoxSizer(wxHORIZONTAL);
	this->SetSizer(szRoot);
	this->SetAutoLayout(true);


	
	//Data
	wxArrayString statusStates;
	statusStates.Add(wxT("Open"));
	statusStates.Add(wxT("Done"));


	//Controls
	inpCreated	= new wxTextCtrl(this, ID_INPCREATED, this->data->created, wxPoint(1,1), wxSize(60,21), 
									wxTE_PROCESS_ENTER, wxDefaultValidator, wxT("inpCreated"));
	inpDue		= new wxTextCtrl(this, ID_INPDUE, this->data->required, wxPoint(63,1), wxSize(60,21),
									wxTE_PROCESS_ENTER, wxDefaultValidator, wxT("inpDue"));
	
	//If wxTE_PROCESS_ENTER is set; then combobox beeps on tab and enter; otherwise does nothing
	drpCategory = new wxComboBox (this, ID_DRPCATEGORY,	this->data->category,	wxPoint(125,1), wxSize(102,21),	
								  categoryArray, wxTE_PROCESS_ENTER, wxDefaultValidator, wxT("drpCategory"));

	inpTodo		= new wxTextCtrl(this, ID_INPTODO,this->data->todo, wxPoint(229,1), wxSize(461,21), 
									wxTE_PROCESS_ENTER, wxDefaultValidator, wxT("inpTodo"));
	
	//Ditto drpCategory
	drpStatus	= new wxComboBox (this, ID_DRPSTATUS, this->data->status, wxPoint(692,1), wxSize(87,21),
									statusStates, wxTE_PROCESS_ENTER, wxDefaultValidator, wxT("drpStatus"));

	szRoot->Add(inpCreated,0,	wxALIGN_CENTER | wxALL,	1);
	szRoot->Add(inpDue,0,		wxALIGN_CENTER | wxALL,	1);
	szRoot->Add(drpCategory,0,	wxALIGN_CENTER | wxALL,	1);
	szRoot->Add(inpTodo,0,		wxEXPAND | wxALL,		1);
	szRoot->Add(drpStatus,0,	wxALIGN_CENTER | wxALL,	1);

}
void pnlTodoItem::OnEnter(wxCommandEvent& event )
{
	wxMessageDialog(this, wxT("Got an enter"), wxT("Notice"), wxOK | wxICON_INFORMATION ).ShowModal();
	event.Skip();
}
CorsairX
Earned a small fee
Earned a small fee
Posts: 15
Joined: Tue Jan 17, 2006 5:30 pm
Contact:

Post by CorsairX »

Any takers? I still can't figure this one out.
metalogic
Super wx Problem Solver
Super wx Problem Solver
Posts: 307
Joined: Fri Oct 08, 2004 8:21 am
Location: Area 51
Contact:

Post by metalogic »

Try this:

Code: Select all

myCombo->Connect( wxID_ANY, wxEVT_CHAR, (wxObjectEventFunction) (wxEventFunction) (wxCharEventFunction) &MyFrame::myComboKeyPress, NULL, this );

void MyFrame::myComboKeyPress( wxKeyEvent& event )
{
	if ( event.GetKeyCode() == WXK_RETURN ) DoSomething();

	event.Skip();
}
Ryan Wilcox
I live to help wx-kind
I live to help wx-kind
Posts: 194
Joined: Mon Aug 30, 2004 1:26 pm
Location: PA, USA
Contact:

Post by Ryan Wilcox »

Or read the wxWidget's source code for the combo box implementation for your platform. Maybe you can find a place and submit a patch enabling it.
Ryan Wilcox
Wilcox Development Solutions
http://www.wilcoxd.com
HeReSY
Earned some good credits
Earned some good credits
Posts: 120
Joined: Fri Sep 17, 2004 8:58 pm
Location: Germany

Post by HeReSY »

Set the style wxTE_PROSESS_ENTER for your combobox, but this is only available under windows.

HeReSY
CorsairX
Earned a small fee
Earned a small fee
Posts: 15
Joined: Tue Jan 17, 2006 5:30 pm
Contact:

Post by CorsairX »

Previously, setting wxTE_PROCESS_ENTER would simply cause navigation through the panel to stop; but with metalogic's code I'm now able to intercept key presses, which is better then half the battle!

Interesting stuff.. I'm still playing around with things, when I come up with a solution I'll be sure to post it up. Thanks all for you help so far.
lester
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Sep 02, 2006 7:24 pm
Location: Ukraine

Post by lester »

I have the same problem - anybody known how process Enter on Mac?
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

lester wrote:I have the same problem - anybody known how process Enter on Mac?
In whatever text areas where user can type stuff, i have solved this with a ugly but working hack - i just read the text held by the component on each event and store it to be used with next text change event. If a text event is processed but the text is still the same as the text there was in the previous event, then the second is most likely that the user pressed enter. I'm not sure it works on other platforms that wxMac, that will depend if pressing enter generates text events even though the text doesnt actually change.
lester
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Sep 02, 2006 7:24 pm
Location: Ukraine

Post by lester »

Thanks, i'll try
Post Reply