How do you detect simultaneous keypresses such as “Ctrl + T” in VB.NET?

前端 未结 5 644
甜味超标
甜味超标 2021-01-02 03:34

I am trying to detect the keys \"Control\" and \"t\" being pressed simultaneously in VB.NET. The code I have so far is as follows:

Private Sub frmTimingP2P_K         


        
5条回答
  •  猫巷女王i
    2021-01-02 03:54

    I had the same problem, but for me to get this to work I had to set the forms KeyPreview property to true. In Visual studio you can change this in the Forms [Design] Property window or changing the property on load.

    Private Sub frmTimingP2P_Load(ByVal sender As System.Object, ByVal e As _ 
                                   System.EventArgs) Handles MyBase.Load
    
        Me.KeyPreview = True
    
    End Sub
    

    then use by using:

    Private Sub frmTimingP2P_KeyDown(ByVal Sender As Object, ByVal e As _ 
                            System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    
    
            If (e.KeyCode = Keys.T AndAlso e.Modifiers = Keys.Control) Then
                MessageBox.Show("Ctrl + T")
            End If
    
    End Sub
    

    or other program logic as provided in the answers above.

提交回复
热议问题