Send keyboard events from one form to another form

my question is pretty simple:

Our C # application has one MainForm with a menu and several keyboard shortcuts associated with menu entries.

Now we need to call the menu items from some child forms. But since MainForm is inactive when one of the child forms is active, shortcuts do not work.

Is there an easy way to distribute all keyboard events from a child form to the Owner form? Or just to a different form at all?

Oh, and we cannot use some low-level Windows files, because we need to also run the application on Mono / Linux.

EDIT: The exact problem is invoking menu items with the same label from a different form. Of course, without updating the code in the forms, if changes are added to the menu of new elements.

+4
source share
7 answers

This is what fixed it for me:

public class MainForm : Form { public bool ProcessCmdKeyFromChildForm(ref Message msg, Keys keyData) { Message messageCopy = msg; messageCopy.HWnd = this.Handle; // We need to assign our own Handle, otherwise the message is rejected! return ProcessCmdKey(ref messageCopy, keyData); } } public class MyChildForm : Form { private MainForm mMainForm; public MyChildForm(MainForm mainForm) { mMainForm = mainForm; } // This is meant to forward accelerator keys (eg. Ctrl-Z) to the MainForm protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (mMainForm.ProcessCmdKeyFromChildForm(ref msg, keyData)) { return true; } return base.ProcessCmdKey(ref msg, keyData); } } 
+1
source

Have you tried something like this?

 ParentForm : Form { public NotifyKeyPress(KeyPressEventArgs e) { OnKeyPress(e); } } ChildForm : Form { ParentForm _parent; public ChildForm(ParentForm parent) { _parent = parent; KeyPress += KeyPressHandler; } public KeyPressHandler(object sender, KeyPressEventArgs e) { if (_parent != null) { _parent.NotifyKeyPress(e); } } } 
+1
source

I think you want to set KeyPreview in the parent form to true

If this property is set to true, the form will receive all KeyPress, KeyDown, and KeyUp events. After the form event handlers have completed the keystroke processing, the keystroke is then assigned to the control with focus. For example, if the KeyPreview property is set to true, and the currently selected control is a TextBox, after pressing the key the event handlers of the form, the TextBox control will receive the key pressed. To handle keyboard events only at the form level and not allow controls to accept keyboard events, set the KeyPressEventArgs.Handled property in your KeyPress event handler to true.

EDIT:

The answer to this question may be useful:

0
source

ToolStrip.AllowMerge property "obtains or sets [...] whether several MenuStrip , ToolStripDropDownMenu , ToolStripMenuItem , and other types can be combined." (MSDN)

This means that you can:

"Use the AllowMerge property to allow children with multiple documents (MDIs) to combine their respective menus in the parent MDI." (Property AllowMerge, Remark, MSDN)

See also:

This, I hope, will help you get what you want. Now I do not know if this is suitable for Windows Forms or if it will work on Linux too after its creation.

0
source

I assume that you are inactive, that you have no focus?

The cleanest way to do this is for each form to display events that relate to manipulations from their menus. When you create forms, sign them on top of each other (either from the child to MainForm or in some way the flow should go). When the menu is clicked, execute an additional event and another form will receive it.

Does it help? I believe this is better than trying to force a message manually, as it will be self-documenting code that forms should respond to each other.

The larger the β€œoff problem” approach, do you need two forms or can you reorganize the user interface design?

0
source

There is a much simpler way to do this. Menu items should trigger the corresponding method calls. You can then call these methods anywhere in the application.

0
source

Instead of binding key shortcuts to menu items in the main form, you can create your own key processing method that responds to shortcuts. Put this method in the main form. Then call this method from all child forms in the key event. The @Adam Driscoll code is very compatible with this approach.

0
source

Source: https://habr.com/ru/post/1311523/


All Articles