【问题标题】:WPF, How to detect Spacebar press in RichTextBoxWPF,如何在 RichTextBox 中检测空格键按下
【发布时间】:2013-10-29 18:18:26
【问题描述】:

在 WPF 中,我有一个 RichTextBox,里面有一个流文档。我需要知道用户何时按下空格键。下面的代码可以在每次按下一个键时显示一个消息框,但不是空格键。如果按 F,例如会显示一个带有 F 的消息框,但是当按下空格时,插入符号只会移动到下一个位置。

private void RichTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
            {
                MessageBox.Show(e.Text);
            }

我在这里缺少什么? 谢谢你的时间:)

【问题讨论】:

    标签: c# wpf visual-studio-2010


    【解决方案1】:

    您可以通过像这样处理PreviewKeyDownPreviewKeyUp 事件来检测空格字符:

    private void PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Space) 
        {
            // The user pressed the space bar
        }
    }
    

    至于PreviewTextInput 事件为什么会忽略空格字符,您可以在Why does PreviewTextInput not handle spaces? 帖子和那里找到的链接中找到一些有趣的信息。

    【讨论】:

    • 谢谢...应该这样做:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    相关资源
    最近更新 更多