wxEVT_KEY_DOWN + other key events does not fire on a wxFrame Topic is solved

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
Calmarius
In need of some credit
In need of some credit
Posts: 6
Joined: Tue Oct 30, 2012 11:18 pm

wxEVT_KEY_DOWN + other key events does not fire on a wxFrame

Post 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.
DavidHart
Site Admin
Site Admin
Posts: 4252
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

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

Post 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
Calmarius
In need of some credit
In need of some credit
Posts: 6
Joined: Tue Oct 30, 2012 11:18 pm

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

Post 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....
Post Reply