none
窗体Form 捕获键盘按键 RRS feed

答案

  • Hi Tony-Best,

    你可以用KeyDown事件来执行, 并且设置KeyPreview = True。

     Private Sub Form10_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            MyBase.KeyPreview = True
        End Sub
    
        Private Sub Form10_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
            MessageBox.Show(e.KeyCode.ToString())
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    2017年7月4日 7:20
    版主

全部回复

  • Hi Tony-Best,

    你可以用KeyDown事件来执行, 并且设置KeyPreview = True。

     Private Sub Form10_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            MyBase.KeyPreview = True
        End Sub
    
        Private Sub Form10_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
            MessageBox.Show(e.KeyCode.ToString())
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    2017年7月4日 7:20
    版主
  • Thanks!十分感谢!

    不过稍微修改上述代码,KeyPress也可以执行,并且不会影响窗体AcceptButton和CancelButton的使用

     Private Sub Form10_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            MyBase.KeyPreview = True
        End Sub
    
        Private Sub Form10_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MyBase.KeyPress
            MessageBox.Show(e.KeyCode)
        End Sub

    2017年7月4日 7:28