【问题标题】:TextBox handle key events and don't pass them onTextBox 处理关键事件并且不传递它们
【发布时间】:2017-05-17 21:32:56
【问题描述】:

我有一个具有全局按键处理功能的 uwp 应用程序。我通过订阅两组不同的事件来做到这一点:

CoreWindow.CharacterReceived (for text keypresses), and
MainPage.KeyDown (for escape, arrow keys, enter, etc.)

但我的应用也有一些文本框。当其中一个有焦点时,我想在几乎所有情况下禁用上述功能。 (箭头键和制表符有时是例外)。

我当然可以通过在我的 TextBox 包装对象中覆盖 OnGotFocus 和 OnLostFocus 并跟踪 TextBox 当前是否具有焦点来做到这一点。

有没有更好的办法?

编辑:我找到了 FocusManager.GetFocusedElement()。这是一种改进,但仍然感觉不理想。

【问题讨论】:

    标签: c# keyboard uwp keyboard-events


    【解决方案1】:

    也许您可以使用FocusManager.GetFocusedElement() 并检查返回的元素是否为TextBox 类型。所以你应该在CoreWindow.CharacterReceivedMainPage.KeyDown 事件处理程序中添加这样的内容:

    EventHandler(parameters)
    {
        if (FocusManager.GetFocusedElement() is TextBox)
        {
            return;
        }
    
        // event handling
    }
    

    【讨论】:

      猜你喜欢
      • 2012-05-19
      • 1970-01-01
      • 1970-01-01
      • 2010-10-14
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-19
      相关资源
      最近更新 更多