Skip to content

Multiple WPFControls causes stack overflow with KeyUp/DownEvent Raise event #82

Description

@lazerW0lf

Problem:
I am using version 4.2.2 and am instantiating multiple GLWpfControls (my application can have multiple tabs with an OGL-based scene) and am encountering a stack overflow upon launching the other GLWpf controller. I've found the root cause of this to be related to the OnKeyUp/OnKeyDown functions that ultimately call RaiseEvent.

Some possible solutions:
I've worked around this in my local repo by checking if the event is handled or not. In my KeyUp/Down callbacks I've set the event's Handled property to true and I made the following edit:

In GLWpfControl.cs:

        internal void OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.OriginalSource != this && !e.Handled)
            {
                KeyEventArgs args = new KeyEventArgs(e.KeyboardDevice, e.InputSource, e.Timestamp, e.Key);
                args.RoutedEvent = Keyboard.KeyDownEvent;
                // \todo I am commenting out because I get an overflow here...
                RaiseEvent(args);
            }
        }

Note that I added the && !e.Handled and am flagging the event as Handled=true in my own key callbacks.

My working theory is that two GLWpfControllers get into an infinite loop by catching/raising the event until the stack overflows.

At the very least, could we get some kind of user-level setting to enable/disable the event handling to avoid this. Or even a setting in Start()'s to protect from this block:

EventManager.RegisterClassHandler(typeof(Control), Keyboard.KeyDownEvent, new KeyEventHandler(OnKeyDown), true);
EventManager.RegisterClassHandler(typeof(Control), Keyboard.KeyUpEvent, new KeyEventHandler(OnKeyUp), true);

Something like

            if ( _settings.RegisterKeyHandlers )
            {
                EventManager.RegisterClassHandler(typeof(Control), Keyboard.KeyDownEvent, new KeyEventHandler(OnKeyDown), true);
                EventManager.RegisterClassHandler(typeof(Control), Keyboard.KeyUpEvent, new KeyEventHandler(OnKeyUp), true);
            }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    4.0bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions