【问题标题】:Replace Empty Value to Keyboard Event将空值替换为键盘事件
【发布时间】:2013-08-28 11:03:24
【问题描述】:

我只关注 WPF 中的键盘上、下、右、左。而且,我不希望发生其他关键事件。参考下面的代码,如何分配或设置其他键为空/dbnull?我在e.Key = Nothing 收到错误消息

Private Sub Button1_KeyUp(sender As Object, e As KeyEventArgs) Handles Button1.KeyUp
    Select Case e.Key
        Case Key.Right
            Keyboard.Focus(Button2)
        Case Key.Down
            Keyboard.Focus(Button3)
        Case Else
            e.Key = Nothing
    End Select
End Sub

【问题讨论】:

    标签: wpf vb.net keyboard


    【解决方案1】:

    您需要使用KeyEventArgs Handled 属性并将其设置为true。根据您想要抑制其他键的程度,您可能需要处理 PreviewKeyDown 事件以过滤掉 Tab 键。

    【讨论】:

    • 示例源代码分享出去。 ` Private Sub ReturnToGame_PreviewKeyDown(sender As Object, e As KeyEventArgs) Handles ReturnToGame.PreviewKeyDown Select Case e.Key Case Key.Left Keyboard.Focus(Button2) Case Key.Up Keyboard.Focus(Button3) End Select e.Handled = True End子`
    【解决方案2】:

    您可以使用 PreviewKeyUp 事件并将 e.Handled 标记为 true

    【讨论】:

    • 感谢您的帮助,PreviewKeyDown 更适合。如果使用 PreviewKeyUp,它实际上会跳转两次。
    猜你喜欢
    • 2017-06-08
    • 2011-01-02
    • 1970-01-01
    • 2021-08-18
    • 2011-06-10
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    相关资源
    最近更新 更多