2011-09-04 101 views

回答

1

爲了找出是否同時按下了兩個按鍵,您需要存儲一個已經按下的按鍵列表,當它們未按下時從列表中刪除按鍵。然後,您可以比較列表中的內容以設置模式,以查看它是否與您正在查找的模式相匹配。

Dim keysPressed as New HashSet(Of Keys) 

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown 

    'Add the pressed key into the list 
    keysPressed.Add(e.KeyCode) 

    If keysPressed.Contains(Keys.W) AndAlso keysPressed.Contains(Keys.A) Then 
     'Add code to take action here 
    End If 

    If keysPressed.Contains(Keys.D) AndAlso keysPressed.Contains(Keys.A) Then 
     'Add code to take action here 
    End If 

    'Add more code to handle actions for multiple keys being pressed 
End Sub 

Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp 
    'Remove the pressed key from the list 
    keysPressed.Remove(e.KeyCode) 
End Sub