catching keyboard pressed button

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
airc
Knows some wx things
Knows some wx things
Posts: 29
Joined: Mon Jan 06, 2014 7:35 am

catching keyboard pressed button

Post by airc »

Hi
the code below works , if you press any key a message box popup showing the key code
the problem is when i add a widget (button , panel ...) to the window , the function on_keydown() no longer works
is this related to maybe losing focus ?

Code: Select all

#include <wx/wx.h>

class cFrame : public wxFrame{
public:
    cFrame(const wxString& title):
	  wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(1024, 600)){

		this->Centre( wxBOTH );
		this->Connect(wxEVT_CHAR, wxKeyEventHandler(on_keydown));
			  
	  }
	  void on_keydown( wxKeyEvent& event ){
		wxMessageBox(wxString::Format(L"KeyDown: %i\n", (int)event.GetKeyCode()));
	  }
};

class MyApp : public wxApp{
  public:
    virtual bool OnInit(){
		cFrame *simple = new cFrame(wxT("wxApp"));
		simple->Show(true);
		return true;	
	}
};

IMPLEMENT_APP(MyApp)
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: catching keyboard pressed button

Post by doublemax »

is this related to maybe losing focus ?
Yes. Only the window/control that has focus receives "normal" key events, and they don't propagate upwards in the window hierarchy like wxCommandEvents do.

The only exception is the wxEVT_CHAR_HOOK event. Please read up on its "special properties" in the docs:
https://docs.wxwidgets.org/trunk/classwx_key_event.html
Use the source, Luke!
airc
Knows some wx things
Knows some wx things
Posts: 29
Joined: Mon Jan 06, 2014 7:35 am

Re: catching keyboard pressed button

Post by airc »

thank you , i'll read it
Post Reply