wxEVT_KEY_DOWN + other key events does not fire on a wxFrame  [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.

wxEVT_KEY_DOWN + other key events does not fire on a wxFrame

Postby Calmarius » Tue Oct 30, 2012 11:29 pm

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

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

Postby DavidHart » Wed Oct 31, 2012 7:46 am

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

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

Postby Calmarius » Wed Oct 31, 2012 9:23 pm

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


Return to C++ Development

Who is online

Users browsing this forum: No registered users and 15 guests