<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Dockable Window with WPF controls, don't receive keyboard input in Inventor Forum</title>
    <link>https://forums.autodesk.com/t5/inventor-forum/dockable-window-with-wpf-controls-don-t-receive-keyboard-input/m-p/9234733#M163647</link>
    <description>&lt;P&gt;Hi&amp;nbsp;@Anonymous,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is a know issue when ElementHost in Windows Forms.&lt;/P&gt;&lt;P&gt;&lt;A title="WPF TextBox not accepting Input when in ElementHost in Window Forms" href="https://stackoverflow.com/questions/835878/wpf-textbox-not-accepting-input-when-in-elementhost-in-window-forms" target="_blank" rel="noopener"&gt;WPF TextBox not accepting Input when in ElementHost in Window Forms&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And it is a&amp;nbsp;side-effect after we modifying the window structure of Dockable Windows.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is no such issue if we directly add the WPF control into the Dockable Window. This could be done by creating a&amp;nbsp;HwndSource object to wrap the WPF control and pass its Handle property to AddChild.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If that could not be done, then please follow the solution in the above post or what is done in an Inventor add-in:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;    public partial class KeyParameterPaletteForm: CommonForm
    {
        …
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            //Configure this form, so that, the form can work well with dockable window
            DockableWindowChildAdapter.SetDockableWindowChildEventHook(this);
        }
    }

    public static class DockableWindowChildAdapter
    {
        private const UInt32 DLGC_WANTARROWS = 0x0001;
        private const UInt32 DLGC_WANTTAB = 0x0002;
        private const UInt32 DLGC_WANTALLKEYS = 0x0004;
        private const UInt32 DLGC_HASSETSEL = 0x0008;
        private const UInt32 DLGC_WANTCHARS = 0x0080;
        private const UInt32 WM_GETDLGCODE = 0x0087;

        /// &amp;lt;summary&amp;gt;
        /// Enable the event for the structure WPF &amp;gt;&amp;gt; WINFORM &amp;gt;&amp;gt; ELEMENTHOST &amp;gt;&amp;gt; CONTROL
        /// Some event cannot be sent to the CONTROL, set hook for the control
        /// http://stackoverflow.com/questions/835878/wpf-textbox-not-accepting-input-when-in-elementhost-in-window-forms
        /// &amp;lt;/summary&amp;gt;
        public static void SetDockableWindowChildEventHook(Form childForm)
        {
            if (childForm == null || childForm.Controls == null || childForm.Controls.Count &amp;lt;= 0)
                return;

            foreach (var control in childForm.Controls)
            {
                var elementHost = control as ElementHost;
                if (elementHost == null || elementHost.Child == null)
                    continue;

                HwndSource visualSourceObject = HwndSource.FromVisual(elementHost.Child) as HwndSource;
                if (visualSourceObject == null)
                    continue;

                visualSourceObject.AddHook(new HwndSourceHook(ChildHwndSourceHook));
            }
        }


        /// &amp;lt;summary&amp;gt;
        /// Handle the key press event from parent window
        /// &amp;lt;/summary&amp;gt;
        private static IntPtr ChildHwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg != WM_GETDLGCODE)
                return IntPtr.Zero;

            handled = true;
            return new IntPtr(DLGC_WANTCHARS | DLGC_WANTARROWS | DLGC_HASSETSEL | DLGC_WANTTAB | DLGC_WANTALLKEYS);
        }
    }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 07 Jan 2020 07:52:17 GMT</pubDate>
    <dc:creator>HelmesHe</dc:creator>
    <dc:date>2020-01-07T07:52:17Z</dc:date>
    <item>
      <title>Dockable Window with WPF controls, don't receive keyboard input</title>
      <link>https://forums.autodesk.com/t5/inventor-forum/dockable-window-with-wpf-controls-don-t-receive-keyboard-input/m-p/9115997#M163643</link>
      <description>&lt;P&gt;I've built a C# based plugin that works in AutoCAD, Revit, Navisworks and Inventor. The plugin creates a dockable "window" in each application to host it's WPF content. This works great in all applications except for Inventor.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In Inventor, I am forced to create a Windows Form that contains a single ElementHost who's child is set to the plugin's main content. This is then placed inside an Inventor &lt;FONT&gt;DockableWindow using AddChild.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;Here's the problem:&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Textboxes in my WFP application do not receive most keyboard input. The only keys which seem to work are control keys and spacebar. I can highlight, copy and paste fine from the textboxes, but I cannot type characters (the typing caret is present).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've read about people with similar issues and many of them mention&amp;nbsp;&lt;FONT&gt;ElementHost.EnableModelessKeyboardInterop&lt;/FONT&gt;. However this will not work for me, because &lt;FONT&gt;EnableModelessKeyboardInterop expects a WPF window, while my plugin only contains a UserControl.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;Another interesting thing is that the plugin work fine in Inventor 2015-2017. Only version 2018 and higer experience the no-keyboard-input issue.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help with this would be greatly appreciated.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2019 00:37:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-forum/dockable-window-with-wpf-controls-don-t-receive-keyboard-input/m-p/9115997#M163643</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2019-10-30T00:37:20Z</dc:date>
    </item>
    <item>
      <title>Re: Dockable Window with WPF controls, don't receive keyboard input</title>
      <link>https://forums.autodesk.com/t5/inventor-forum/dockable-window-with-wpf-controls-don-t-receive-keyboard-input/m-p/9116013#M163644</link>
      <description>&lt;P&gt;Hi @Anonymous,&lt;/P&gt;
&lt;P&gt;It seems a regression to me, will contact project team for a better understanding then.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2019 00:52:16 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-forum/dockable-window-with-wpf-controls-don-t-receive-keyboard-input/m-p/9116013#M163644</guid>
      <dc:creator>Xun.Zhang</dc:creator>
      <dc:date>2019-10-30T00:52:16Z</dc:date>
    </item>
    <item>
      <title>Re: Dockable Window with WPF controls, don't receive keyboard input</title>
      <link>https://forums.autodesk.com/t5/inventor-forum/dockable-window-with-wpf-controls-don-t-receive-keyboard-input/m-p/9222245#M163645</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Today I encountered same trouble and found this post.&lt;/P&gt;&lt;P&gt;I tried to add WPF Window to DockablwWindow's child directly, and it works, but this issue still laies.&lt;/P&gt;&lt;P&gt;(Of course, &lt;FONT&gt;EnableModelessKeyboardInterop &lt;/FONT&gt;was used.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now I have reported this issue to the Inventor beta forum.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;=====&lt;/P&gt;&lt;P&gt;&lt;A href="https://www.freeradical.jp" target="_blank" rel="noopener"&gt;Freeradical&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Hideo Yamada&lt;/P&gt;</description>
      <pubDate>Sat, 28 Dec 2019 08:03:35 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-forum/dockable-window-with-wpf-controls-don-t-receive-keyboard-input/m-p/9222245#M163645</guid>
      <dc:creator>HideoYamada</dc:creator>
      <dc:date>2019-12-28T08:03:35Z</dc:date>
    </item>
    <item>
      <title>Re: Dockable Window with WPF controls, don't receive keyboard input</title>
      <link>https://forums.autodesk.com/t5/inventor-forum/dockable-window-with-wpf-controls-don-t-receive-keyboard-input/m-p/9234614#M163646</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/555733"&gt;@HideoYamada&lt;/a&gt;&amp;nbsp;@Anonymous, thanks your feedback and sorry for the inconvenience. This is a tech limitation, we need additional handling in the plugin. Our developer will put more details about resolution here later.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jan 2020 05:05:02 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-forum/dockable-window-with-wpf-controls-don-t-receive-keyboard-input/m-p/9234614#M163646</guid>
      <dc:creator>JarFu</dc:creator>
      <dc:date>2020-01-07T05:05:02Z</dc:date>
    </item>
    <item>
      <title>Re: Dockable Window with WPF controls, don't receive keyboard input</title>
      <link>https://forums.autodesk.com/t5/inventor-forum/dockable-window-with-wpf-controls-don-t-receive-keyboard-input/m-p/9234733#M163647</link>
      <description>&lt;P&gt;Hi&amp;nbsp;@Anonymous,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is a know issue when ElementHost in Windows Forms.&lt;/P&gt;&lt;P&gt;&lt;A title="WPF TextBox not accepting Input when in ElementHost in Window Forms" href="https://stackoverflow.com/questions/835878/wpf-textbox-not-accepting-input-when-in-elementhost-in-window-forms" target="_blank" rel="noopener"&gt;WPF TextBox not accepting Input when in ElementHost in Window Forms&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And it is a&amp;nbsp;side-effect after we modifying the window structure of Dockable Windows.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is no such issue if we directly add the WPF control into the Dockable Window. This could be done by creating a&amp;nbsp;HwndSource object to wrap the WPF control and pass its Handle property to AddChild.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If that could not be done, then please follow the solution in the above post or what is done in an Inventor add-in:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;    public partial class KeyParameterPaletteForm: CommonForm
    {
        …
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            //Configure this form, so that, the form can work well with dockable window
            DockableWindowChildAdapter.SetDockableWindowChildEventHook(this);
        }
    }

    public static class DockableWindowChildAdapter
    {
        private const UInt32 DLGC_WANTARROWS = 0x0001;
        private const UInt32 DLGC_WANTTAB = 0x0002;
        private const UInt32 DLGC_WANTALLKEYS = 0x0004;
        private const UInt32 DLGC_HASSETSEL = 0x0008;
        private const UInt32 DLGC_WANTCHARS = 0x0080;
        private const UInt32 WM_GETDLGCODE = 0x0087;

        /// &amp;lt;summary&amp;gt;
        /// Enable the event for the structure WPF &amp;gt;&amp;gt; WINFORM &amp;gt;&amp;gt; ELEMENTHOST &amp;gt;&amp;gt; CONTROL
        /// Some event cannot be sent to the CONTROL, set hook for the control
        /// http://stackoverflow.com/questions/835878/wpf-textbox-not-accepting-input-when-in-elementhost-in-window-forms
        /// &amp;lt;/summary&amp;gt;
        public static void SetDockableWindowChildEventHook(Form childForm)
        {
            if (childForm == null || childForm.Controls == null || childForm.Controls.Count &amp;lt;= 0)
                return;

            foreach (var control in childForm.Controls)
            {
                var elementHost = control as ElementHost;
                if (elementHost == null || elementHost.Child == null)
                    continue;

                HwndSource visualSourceObject = HwndSource.FromVisual(elementHost.Child) as HwndSource;
                if (visualSourceObject == null)
                    continue;

                visualSourceObject.AddHook(new HwndSourceHook(ChildHwndSourceHook));
            }
        }


        /// &amp;lt;summary&amp;gt;
        /// Handle the key press event from parent window
        /// &amp;lt;/summary&amp;gt;
        private static IntPtr ChildHwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg != WM_GETDLGCODE)
                return IntPtr.Zero;

            handled = true;
            return new IntPtr(DLGC_WANTCHARS | DLGC_WANTARROWS | DLGC_HASSETSEL | DLGC_WANTTAB | DLGC_WANTALLKEYS);
        }
    }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jan 2020 07:52:17 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-forum/dockable-window-with-wpf-controls-don-t-receive-keyboard-input/m-p/9234733#M163647</guid>
      <dc:creator>HelmesHe</dc:creator>
      <dc:date>2020-01-07T07:52:17Z</dc:date>
    </item>
    <item>
      <title>Re: Dockable Window with WPF controls, don't receive keyboard input</title>
      <link>https://forums.autodesk.com/t5/inventor-forum/dockable-window-with-wpf-controls-don-t-receive-keyboard-input/m-p/9239358#M163648</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Referring to Helmes's post, I have written following code. This works well for me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;public void Activate(ApplicationAddInSite addInSiteObject, bool firstTime)
{
    // Setup my WPF Window.
    var wpfWindow = new WpfWindow();
    wpfWindow.WindowStyle = System.Windows.WindowStyle.None;
    wpfWindow.ResizeMode = System.Windows.ResizeMode.NoResize;
    wpfWindow.Visibility = System.Windows.Visibility.Visible;

    // Get WPF Window's handle.
    var helper = new WindowInteropHelper(wpfWindow);
    helper.EnsureHandle();
    var handle = helper.Handle;

    // Create Dockable Window.
    var dockableWindow = InventorApplication.UserInterfaceManager.DockableWindows.Add(System.Guid.NewGuid().ToString(), "Test", "Test");
    dockableWindow.AddChild(handle);

    // Set key hook.
    HwndSource.FromHwnd(handle).AddHook(WndProc);
}

private const UInt32 DLGC_WANTARROWS = 0x0001;
private const UInt32 DLGC_WANTTAB = 0x0002;
private const UInt32 DLGC_WANTALLKEYS = 0x0004;
private const UInt32 DLGC_HASSETSEL = 0x0008;
private const UInt32 DLGC_WANTCHARS = 0x0080;
private const UInt32 WM_GETDLGCODE = 0x0087;

private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    if (msg == WM_GETDLGCODE)
    {
        handled = true;
        return new IntPtr(DLGC_WANTCHARS | DLGC_WANTARROWS | DLGC_HASSETSEL | DLGC_WANTTAB | DLGC_WANTALLKEYS);
    }
    return IntPtr.Zero;
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;=====&lt;/P&gt;&lt;P&gt;&lt;A href="https://www.freeradical.jp" target="_blank" rel="noopener"&gt;Freeradical&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Hideo Yamada&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jan 2020 03:27:42 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-forum/dockable-window-with-wpf-controls-don-t-receive-keyboard-input/m-p/9239358#M163648</guid>
      <dc:creator>HideoYamada</dc:creator>
      <dc:date>2020-01-09T03:27:42Z</dc:date>
    </item>
    <item>
      <title>Re: Dockable Window with WPF controls, don't receive keyboard input</title>
      <link>https://forums.autodesk.com/t5/inventor-forum/dockable-window-with-wpf-controls-don-t-receive-keyboard-input/m-p/9239380#M163649</link>
      <description>&lt;P&gt;I forgot to write the line initializing the variable 'InventorApplication'.&lt;/P&gt;&lt;PRE&gt;public void Activate(ApplicationAddInSite addInSiteObject, bool firstTime)
{
    InventorApplication = addInSiteObject.Application;
    ...&lt;/PRE&gt;&lt;P&gt;// In my actual code, it will be initialized by my own middle ware &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jan 2020 03:56:16 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-forum/dockable-window-with-wpf-controls-don-t-receive-keyboard-input/m-p/9239380#M163649</guid>
      <dc:creator>HideoYamada</dc:creator>
      <dc:date>2020-01-09T03:56:16Z</dc:date>
    </item>
    <item>
      <title>Re: Dockable Window with WPF controls, don't receive keyboard input</title>
      <link>https://forums.autodesk.com/t5/inventor-forum/dockable-window-with-wpf-controls-don-t-receive-keyboard-input/m-p/9239435#M163650</link>
      <description>&lt;P&gt;Glad to know it works!&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":smiling_face_with_smiling_eyes:"&gt;😊&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jan 2020 05:20:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-forum/dockable-window-with-wpf-controls-don-t-receive-keyboard-input/m-p/9239435#M163650</guid>
      <dc:creator>JarFu</dc:creator>
      <dc:date>2020-01-09T05:20:20Z</dc:date>
    </item>
  </channel>
</rss>

