2011-09-04 68 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