KeyDown event not fired on floating window

Docking/MDI for Windows Forms Forum

Posted 2 years ago by Amyn Virani
Version: 16.1.0331
Platform: .NET 4.5
Environment: Windows 10 (64-bit)
Avatar

Hi.

I am facing an issue with getting events (specifically KeyDown) from a floating window when the layout is first loaded from a file.

  1. Open SampleApplication-CSharp->Product Samples->Docking Samples->Demo->Docking Features->MainForm.cs
  2. Add Key_Down event for markupLabelToolWindow and added the following code inside: MessageBox.Show("Key_Down");
  3. Ran the Docking & MDI sample
  4. Undocked the markupLabelToolWindow title "Welcome to WinForms Studio" and therefore that window is not floating
  5. Pressed a random key while focused on the markupLabelToolWindow and the popup appeared.
  6. Saved the layout using File-> Save Tool Window Layout
  7. Loaded the same layout file stored above by File-> Load Tool Window Layout
  8. Pressed a random key while focused on the markupLabelToolWindow but there was no popup.

Let me know if you are able to reproduce the above behavior at your end and any suggestions as to how to go about this behavior.

Thanks.

Comments (9)

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

I think the problem here is that ToolWindow instances might get recreated on layout loads so you don't really want to attach to events like KeyDown directly on them.  Instead, I'd suggest that you attach to KeyDown on the root content control that is displayed within the tool window.


Actipro Software Support

Posted 2 years ago by Amyn Virani
Avatar

Could you elaborate on "attach KeyDown on the root content"? Specifically what changes to make in order to get the KeyDown event on the floating window in the scenario I described in the post above.

Thanks.

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

I'm referring to whatever root control you use as the content of the tool window.  In your specific scenario, the root control of markupLabelToolWindow is markupLabel.  But perhaps you put some other UserControl, etc. to wrap that and put your KeyDown handler on it.


Actipro Software Support

Posted 2 years ago by Amyn Virani
Avatar

Thank you for the quick response.

I am still unable to capture the event. I attached a KeyDown event to the markupLabel in the sample application provided, restored the same layout file as described above, still did not get the popup message.

However, as soon as I press (Shit+Tab) after loading the layout and press any key, the message popup appears. It seems like the focus is not set to the correct object.

Note that I am only using the sample project provided with Actipro setup and I don't see a reason why the KeyDown event should not be captured as expected.

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Our markup label control is just a display only control and doesn't really capture focus.  That could be part of what you just saw.  You might need to have some child control in the tool window that is actually focusable for KeyDown to be caught at that level.  I'd recommend swapping a TextBox or other focusable control in for the MarkupLabel instead and see if you still have the problem then.


Actipro Software Support

Posted 2 years ago by Amyn Virani
Avatar

Adding a text box inside and attaching a KeyDown event to the text box does show the popup but I am still having a hard time understanding why the orignal case is not throwing events initially. As soon as you dock and undock the markupLabelToolWindow, the events are getting fired as expected.

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

KeyDown only fires where the focus is within.  The ToolWindow control is marked as not Selectable, meaning when it gets focused (such as title bar clicks, etc.), it is supposed to be forwarding focus down to a focusable child control.  But since MarkupLabel isn't a focusable control, I'm not sure where focus goes.  If you have a focusable control somewhere in the ToolWindow, and have the KeyDown event attached to the root content control of the ToolWindow, then the KeyDown should work.  That's why I had you try the TextBox.  Because that is a focusable control and thus ensures that focus moves properly into the content of the ToolWindow in various scenarios.


Actipro Software Support

Posted 2 years ago by Amyn Virani
Avatar

I understand what you are saying with having a control that is focusable but that is not always the case. And if that were the case, the behavior should be consistent. However, docking the markupLabelToolWindow and floating it does start throwing the events although as per your explanation, it shouldn't be throwing any events since there are no focusable control on them.

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

I did some debugging and in this scenario, after the layout load, it appears the ToolWindowContainer has focus, which is the root control it the rafting Form.  Since that control is above any ToolWindow or content control in hierarchy, that's why KeyDown doesn't fire.

When you click the title bar of a docked tool window, it is manually focusing the ToolWindow.  Since there is no focusable child control in this scenario, WinForms is falling back to focus the ToolWindow.  In the case of a floating ToolWindow after a layout load, the title bar there is a system title bar and not our docked title bar.  So it's running different logic when clicked.  We added some code for the next maintenance release so that when the floating title bar is clicked, if the ToolWindowContainer has focus (which is the problem scenario here) it will focus the ToolWindow (or a focusable child control if available).  This should help.


Actipro Software Support

The latest build of this product (v2018.1 build 0341) was released 6 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.