简体   繁体   English

覆盖RichEditBox上的键盘快捷键?

[英]Overriding keyboard shortcuts on a RichEditBox?

Is there a way to disable, or better yet override, keyboard shortcuts on the WinRT RichEditBox control? 有没有办法在WinRT RichEditBox控件上禁用或更好地覆盖键盘快捷键? I want to be able to disable the bold and italic formatting when you press Ctrl-B and Ctrl-I. 我希望能够在按Ctrl-B和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. 我正避免使用常规纯文本框,因为我想使用RichEditBox中的格式选项为文本添加语法高亮。 If the user can manipulate the styling within the box, that won't work. 如果用户可以操作框内的样式,那将无法工作。

Thank you! 谢谢!

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. 最后我在另一个问题中找到了答案:在触发KeyDown事件之前调用文本控件的OnKeyDown方法,因此您必须创建RichEditBox的子类并覆盖OnKeyDown方法,而不是监听KeyDown事件。 Then in your XAML markup or wherever you're instantiating the RichEditBox , use your custom subclass instead. 然后在您的XAML标记中或在您实例化RichEditBox ,请使用您的自定义子类。 As a somewhat-related example, I created an override of TextBox that prevents undo and redo operations: 作为一个有点相关的例子,我创建了一个TextBox的覆盖,可以防止撤消和重做操作:

[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);
        }
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM