Detecting keyboard shortcuts in WinForm swallowed by host mfc accelerators

549 views Asked by At

I have a MFC MDI app which defines Ctrl-F as a keyboard accelerator. This app hosts a WinForm dialog. My problem is I want to catch Ctrl-F from this child WinForm dialog but the MFC mainframe seems to be swallowing it.

  • I can detect other keyboard shortcuts in the WinForm dialog, if they are not defined in the accelerators table
  • If the edit dialog was MFC too, I think I could define and load an accelerator for this dialog too, and then use ProcessMessageFilter
  • in WinForm i've tried both overriding ProcessCmdKey and listening KeyDown events to no avail

Do you think there is a way to receive those key press in the WinForm child?

2

There are 2 answers

0
ekt On BEST ANSWER

I've found a solution that works. When showing the c# WinForm, use the overload

Form.Show(IWin32Window owner)

and pass the MFC mainframe Hwnd as the owner. Now everything is working as expected.

0
HVar On

I think defining your own message loop in WinForm child might work. e.g.

    while(true) {
        Message m;
        GetMessage(out m);
        if (m.Msg == WM_QUIT) 
break;
        DispatchMessage(m);
      }

I guess when you type CTRL + F on WinForm then the message is retrieved by the main message loop i.e. your MFC mainframe app and since the focus is on WinForm it doesn't do anything. If you have your own message loop and accelerator key for WinForm then it might work.