Search code examples
c++wxwidgets

What does wxWidgets' EVT_CHAR_HOOK do?


I am maintaining a wxWidgets C++ application that uses EVT_CHAR_HOOK to capture key events in a high level window. I can't find any real documentation for this event but I can surmise that it intercepts key events in some way that has priority over the "standard" key events. One disturbing thing that I just discovered is that, if this hook is in place, any accelerator keys that might have been defined will no longer fire their events even if the event handler calls Skip() on the event. I've also seen some posts when searching on google that seemed to suggest that EVT_CHAR_HOOK may not be supported on all platforms. Is this true and should I be using it?


Solution

  • I just looked into src/gtk/window.cpp and found this piece:

            // Implement OnCharHook by checking ancestor top level windows
            wxWindow *parent = win;
            while (parent && !parent->IsTopLevel())
                parent = parent->GetParent();
            if (parent)
            {
                event.SetEventType( wxEVT_CHAR_HOOK );
                ret = parent->HandleWindowEvent( event );
            }
    
            if (!ret)
            {
                event.SetEventType(wxEVT_CHAR);
                ret = win->HandleWindowEvent( event );
            }
    

    So, maybe you just need to return false from your OnCharHook event handler?

    In accordance to one of wx Mailing list post:

    If you want the frame to catch all char events, use EVT_CHAR_HOOK, otherwise the events get sent to the children. If you want to catch stuff like escape, you need to set wxWANTS_CHARS flag on the frame. Also, make sure you call event.Skip() on chars you don't process.

    But another post states:

    ...don't use EVT_CHAR_HOOK(), it is a hack. Moreover, it is a Windows-specific hack and I'm personally not at all interested in getting it to work right. If anything, I'd like to deprecate it and get rid of it completely. VZ.

    This post may be of interest to you.