WPF shortcuts are activating when typing in HwndHost parented rich edit control

971 Views Asked by At

I am writing a WPF app that wraps up a legacy MFC richtext editor. I have wrapped the richtext editor in a HwndHost. The HwndHost control sits alongside a WPF TabControl.

Screenshot of running app at http://www.kempy.co.uk/code/Test.png

Apart from the focusing issues and navigation issues (up/down/left/right/tab/enter) which I have managed to resolve, I still have one showstopping issue:

Whenever a character is typed in the rich text control that is a shortcut key on the panel, the shortcut is activated and the richedit control never gets that character. A simple test project is attached to demonstrate the behaviour at http://www.kempy.co.uk/code/Test.zip. Pressing P or C in the rich edit control will focus the panel or click the button, even though the rich text control has keyboard focus.

FYI, here is the code that allows the rich edit control to handle Tab, Enter, Up, Down, Left, Right

#undef TranslateAccelerator
virtual bool TranslateAccelerator (System::Windows::Interop::MSG% msg, ModifierKeys modifiers) = IKeyboardInputSink::TranslateAccelerator
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    if (msg.message == WM_KEYDOWN)
    {
        // we want tabs when the rich edit is focused, send rich edit control a tab WM_CHAR 
        if (msg.wParam == (IntPtr)VK_TAB)
        {
            if (GetFocus() == m_pRichEdit->GetSafeHwnd())
            {
                m_pRichEdit->SendMessage(WM_CHAR, '\t', 0);
                return true;
            }
            else
            {
                return false;
            }
        }
        else if (
            msg.wParam == (IntPtr)VK_UP
            || msg.wParam == (IntPtr)VK_DOWN
            || msg.wParam == (IntPtr)VK_LEFT
            || msg.wParam == (IntPtr)VK_RIGHT
            || msg.wParam == (IntPtr)VK_RETURN)
        {
            // need cursor keys and enter/return, send KEYDOWN messages to rich edit control
            m_pRichEdit->SendMessage(msg.message, msg.wParam.ToInt32(), msg.lParam.ToInt32());
            return true;
        }

    }
    return false;
}
1

There are 1 best solutions below

0
On

Do you need to override OnMnemonic?

Dr Dobs article about interop...

http://drdobbs.com/windows/197003872?pgno=3