4

Is there a way to disable, or better yet override, keyboard shortcuts on the WinRT RichEditBox control? I want to be able to disable the bold and italic formatting when you press Ctrl-B and Ctrl-I.

I'm avoiding using a regular plain TextBox because I want to use the formatting options in the RichEditBox to add syntax highlighting to the text. If the user can manipulate the styling within the box, that won't work.

Thank you!

Courtney
  • 166
  • 9
  • Did you try the solution from the following question: http://stackoverflow.com/questions/260716/override-shortcut-keys-on-net-richtextbox – jsirr13 Feb 16 '15 at 23:34
  • @jsirr13 That applies to WinForms. The RichEditBox in WinRT doesn't have the ModifierKeys property, nor does the Control base class. – Courtney Feb 16 '15 at 23:57
  • You might try handling the [`KeyDown`](https://msdn.microsoft.com/en-ca/windows.ui.xaml.uielement.keydown) event of your [`RichExitBox`](https://msdn.microsoft.com/en-ca/windows.ui.xaml.controls.richeditbox) and discarding the appropriate control keys. – dbc Feb 17 '15 at 00:28
  • @dbc I've tried setting `Handled` on `KeyRoutedEventArgs`for both `KeyDown` and `KeyUp` to true. This prevents me from typing anything at all into the box... but I'm still able to bold and italicize any text that was already there. – Courtney Feb 17 '15 at 00:35
  • Maybe something along the lines of this: http://stackoverflow.com/questions/21203494/richeditbox-using-ctrli-to-set-italic-text-deletes-the-text. You can try to check if the control key is currently held down with this: http://stackoverflow.com/questions/13001215/detect-if-modifier-key-is-pressed-in-keyroutedeventargs-event – dbc Feb 17 '15 at 00:51
  • @dbc I've played around with those and there may be some promise there as far as detecting when to override the key command. The problem that I'm facing is that even if I got that to work, I don't know how to actually override the key command. That's what setting `Handled` to `true` should have done, and it works for everything *except* bolding and italicizing text. – Courtney Feb 17 '15 at 02:50

1 Answers1

2

At long last I found the answer in another question: the OnKeyDown method of a text control gets called before the KeyDown event is fired, so rather than listening to the KeyDown event, you must create a subclass of RichEditBox and override the OnKeyDown method. Then in your XAML markup or wherever you're instantiating the RichEditBox, use your custom subclass instead. As a somewhat-related example, I created an override of TextBox that 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);
        }
    }
};
Community
  • 1
  • 1
sethobrien
  • 969
  • 7
  • 13