How can WPF child window receive escape key

Hello, I have some problem with receiving key event.

I made a plugin with reactiveui and wpf.

And in my wpf code, I created a new window and show like modal

But in modal, Keydown event does not occur when I press the escape key.

I tried and searched it many days but I can’t find how can I solve.

I think if I use ElementHost.EnableModelessKeyboardInterop function this problem will fixed. but I don’t know how to do it.

protected override LoadReturnCode OnLoad(ref string errorMessage)
        {
            AppBootstrapper appBootStrapper = new AppBootstrapper();

            var mainView = new MainView();
            ElementHost host = new ElementHost
            {
                Dock = System.Windows.Forms.DockStyle.Fill,
                Child = mainView
            };
            MRhinoUiDockBar dockBar = new MRhinoUiDockBar(Guid.NewGuid(), "myDockBar", host);

            MRhinoDockBarManager.CreateRhinoDockBar(
                this,
                dockBar,
                true,
                MRhinoUiDockBar.DockLocation.left,
                MRhinoUiDockBar.DockStyle.left_and_right,
                new System.Drawing.Point(200, 200));

            return LoadReturnCode.Success;
        }

Above code is my plugin code.

MainView class is UserContrl(ReactiveUserControl)

and in MainView, I open a new window and show. Then escape key is not raised in KeyDown Event.

  1. how can I implement ElementHost.EnableModelessKeyboardInterop

  2. if 1 approach is not working, how can I solve it.

  3. Additional Question, How can I add my window not user control in Rhino Dock Bar.

  4. Can I add app.xaml in rhino plugin ?? or how can I implement app.xaml functions in rhino plugin?

I really hope to solve this and Sorry my poor english … TT

Thank you for read this topic

Hi @kyungmin.so,

Are you using Rhino 5 or Rhino 6? If you are using Rhino 6, then you should not be using Rhino_DotNet. Rather, you should be using RhinoCommon.

https://developer.rhino3d.com/guides/rhinocommon/

MRhinoUiDockBar is part of Rhino_DotNet and considered obsolete in Rhino 6.

– Dale

Hello @dale

I’m using Rhino6. I change MRhinoUiDockBar to Dockbar using RhinoWindows.Controls.

public class MainViewDockBar : DockBar
    {
        public MainViewDockBar()
            : base(MyRhinoCAMPlugIn.Instance, BarId, "WPF")
        {
            var window = new MainWindow();
            ElementHost.EnableModelessKeyboardInterop(window);
            this.SetContentControl(new WpfHost(window, null));
        }

        public static Guid BarId => new Guid("{c520731e-376a-4d82-975a-403664fca2fc}");
    }

window xaml only have

<Grid>
        <view:MainView x:Name="MainView"/>
    </Grid>

But, MainView is not shown.

if I change this.SetContentControl(new WpfHost(window, null)) to this.SetContentControl(new WpfHost(new MainView(), null)) then main view is shown on dockbar.

Should I set only UserControl??

And even though change code, esc keydown event is not raised on child window of main view.

What is wrong to me ㅜㅜ

Thank you for your replying

  • Kyungmin

Hi @kyungmin.so,

Have a look at the SampleCsWpf sample, which is part of the Rhino deveoper samples available on GitHub. The sample demonstrates how to embed a WPF control into a Rhino docking panel.

Does this help?

– Dale

Hi @dale

Thank you for replying.

About SampleCsWpf I already read it, but it demonstrates embedding a WPF control into rhino panel.

But I want my plugin its own dockbar.

And my main purpose of this topic is child window which created in my wpf user control can’t receive escape key down event.

I already tried it through SampleCsWpf approach which embeds wpf control into a rhino docking panel.
But it also can’t raise escape key down event in child window.

My program scenario is that if user doubleclick some row of listview, then show new window for clicked row setting.

Then when focusing exist in a new window, if esc key down is raised, new window will be closed.

But esc key down event is not raised and only esc key up event is raised.

But because of specific logic, I need esc key down event.

In SampleCsWpf it use ShowSemiModal for show modal. And should I use ShowSemiModal for open new window??

Thank you for your help

  • Kyungmin

Hi @kyungmin.so,

Gettting escape key (and tab key) notifications from a modeless user interface presented in a DLL is a bit of a challenge. If you google “dll modeless dialog tab” you’ll see the issue comes frequently.

The only thing you can really do is hook the Windows keyboard. In our developer samples repository on GitHub, we have a WinForms sample that demonstrates this. you should be able to apply this workaround to WPF.

SampleCsModelessTabForm

– Dale

Hi @dale

I have a question.

image

Above picture is my solution directory.

When i test my application, I set start project to WPF.

And creating new child window function is in presentation project.

Then, if I start my application from WPF project, then WPF project will also reference presentation dll.

But in this case, Esc KeyDown is not fired.

Isn’t it related “dll modeless diaglog tab”

Really Thank you for your help

-Kyungmin

Hi @kyungmin.so,

Sorry, I don’t know how to help.

– Dale