Talk:Catching key events globally
Jump to navigation
Jump to search
2010-07-07
http://wiki.wxwidgets.org/Catching_key_events_globally
An application key. +- for magnifier.
Most of stuff on the article is too compicated. The one, which creates 'new myself' and puts it in all childs, looks especially hacky. But a custom event handler in the App, appears isn't so complex. // bad, it receives all events, incl. if it is a keypress upon a dialog box // everything succeed unexpectedly : have just to allow propagations, and a dialog events will not go together, but will btw be filtered out int App::FilterEvent (wxEvent& event) { if (event.GetEventType () == wxEVT_CHAR) { // have only go up once; then this subroutine will be called again // event.ResumePropagation (100); event.ResumePropagation (1); // don't return 0 or 1, have to let it to propagate up instead // return 1; } return -1; } BEGIN_EVENT_TABLE(Frame, wxFrame) // *normally does not occur, since the frame is accounted to have no focus EVT_CHAR(Frame::OnChar) END_EVENT_TABLE() void Frame::OnChar (wxKeyEvent& event) { if (! event.GetSkipped ()) { #if 1 m_display->OnChar (event); // bad, it will propagate up, and come at here again :) #else wxEvtHandler* handler; handler = m_display->GetEventHandler (); handler->ProcessEvent (event); #endif } // stop here (although not matters) event.Skip (); } // some window, somewhere in some splitter BEGIN_EVENT_TABLE(Display, wxScrolledWindow) EVT_CHAR(Display::OnChar) END_EVENT_TABLE() // keys for my beautiful magnifier void Display::OnChar (wxKeyEvent& event) { switch (event.GetKeyCode ()) { // strange, some disgarmony is here : not WXK_ADD, but '+' or '=' // as an alternative is offered only 'GetRawKeyCode ()' case WXK_ADD: case '=': case '+': ChangeTimeResolution (1, 2.0); return; case WXK_SUBTRACT: case '-': ChangeTimeResolution (0, 2.0); return; } event.Skip (); }
Alex_I 21:52, 6 July 2010 (UTC)