Overriding keyboard shortcuts on a RichEditBox?

Is there a way to disable or better override the keyboard shortcuts in the WinRT RichEditBox control? I want to turn off bold and italic formatting when you press Ctrl-B and Ctrl-I.

I avoid using plain text TextBox because I want to use formatting options in RichEditBox to add syntax highlighting to text. If the user can manipulate the style in the field, this will not work.

Thank!

+4
source share
1 answer

Finally, I found the answer to another question : the method of the OnKeyDowntext control is called before the event KeyDown, so than listening to the event KeyDown, you have to create a subclass RichEditBoxand override the method OnKeyDown. Then in your XAML markup or wherever you instantiate RichEditBox, use your own subclass instead. As a somewhat related example, I created an override TextBoxthat prevents undo and redo operations:

[Windows::Foundation::Metadata::WebHostHidden]
public ref class BetterTextBox sealed : public Windows::UI::Xaml::Controls::TextBox
{
public:
    BetterTextBox() {}
    virtual ~BetterTextBox() {}
    virtual void OnKeyDown(Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e) override
    {
        Windows::System::VirtualKey key = e->Key;
        Windows::UI::Core::CoreVirtualKeyStates ctrlState = Windows::UI::Core::CoreWindow::GetForCurrentThread()->GetKeyState(Windows::System::VirtualKey::Control);
        if ((key == Windows::System::VirtualKey::Z || key == Windows::System::VirtualKey::Y) &&
            ctrlState != Windows::UI::Core::CoreVirtualKeyStates::None)
        {
            e->Handled = true;
        }

        // only call the base implementation if we haven't already handled the input
        if (!e->Handled)
        {
            Windows::UI::Xaml::Controls::TextBox::OnKeyDown(e);
        }
    }
};
+2
source

All Articles