Page 1 of 1

wxEVT_KEY_DOWN + other key events does not fire on a wxFrame

Posted: Tue Oct 30, 2012 11:29 pm
by Calmarius
I probably missing something very obvious. But Google search did not show up anything relevant (so it must be sooo obvious that no one have ever had problems with it).

I derived a class from wxFrame to make a window.
I connected a key down event on it in its constructor:

Code: Select all

	
Connect(wxEVT_KEY_DOWN,(wxObjectEventFunction)&CrystalEditorFrame::OnKeyDown);
When I press a key while the window is active, the event simply does not fire (set a breakpoint, it's not hit). That's the story. Mouse events work.

I'm using wxGTK and wxWidgets 2.8 on Ubuntu 10.04.

Re: wxEVT_KEY_DOWN + other key events does not fire on a wxF

Posted: Wed Oct 31, 2012 7:46 am
by DavidHart
Hi,

Frames seldom have keyboard focus, and wxKeyEvent doesn't propagate to parent controls (see the Event Overview). So if your frame has any child controls, one of them will have focus. As you don't Connect() to that control, the event won't be handled by the frame.

Even without child controls, the frame won't automatically have focus; you'd need explicitly to SetFocus() to it.

For workarounds, see this wxWiki page.

Regards,

David

Re: wxEVT_KEY_DOWN + other key events does not fire on a wxF

Posted: Wed Oct 31, 2012 9:23 pm
by Calmarius
wxGTK FAQ says wxFrames cannot receive keyboard events at all (but it discusses EVT_CHAR so that's why google results didn't show up). You must create a wxPanel inside it, and SetFocus() on it. That's counter intuitive and I needed to spend 2 hours to find it out....