"iwdu15" <jmmgoalsteratyahoodotcom> schrieb
hi, im making a program that makes a picturebox move according to
key presses...but what if the user pushes say down and right at the
same time. the keypress event only responds to once key
pressed...how can i respond to two keys pressed at once so my
picturebox will move diagonally?
"Take this": (one possible solution)
Private Class KeyCombination
Public Left, Right, Down, Up As Boolean
Public Overloads Function Equals( _
ByVal obj As KeyCombination) As Boolean
Return Me.Left = obj.Left AndAlso Me.Right = obj.Right _
AndAlso Me.Down = obj.Down AndAlso Me.Up = obj.Up
End Function
Public Sub New()
'empty
End Sub
Public Sub New(ByVal Left As Boolean, ByVal Right As Boolean, _
ByVal Down As Boolean, ByVal Up As Boolean)
Me.Left = Left
Me.Right = Right
Me.Up = Up
Me.Down = Down
End Sub
End Class
Private Class KeyCombinations
Public Shared ReadOnly None As New KeyCombination( _
False, False, False, False)
Public Shared ReadOnly Up As New KeyCombination( _
False, False, False, True)
Public Shared ReadOnly Down As New KeyCombination( _
False, False, True, False)
Public Shared ReadOnly Left As New KeyCombination( _
True, False, False, False)
Public Shared ReadOnly Right As New KeyCombination( _
False, True, False, False)
Public Shared ReadOnly LeftUp As New KeyCombination( _
True, False, False, True)
Public Shared ReadOnly RightUp As New KeyCombination( _
False, True, False, True)
Public Shared ReadOnly LeftDown As New KeyCombination( _
True, False, True, False)
Public Shared ReadOnly RightDown As New KeyCombination( _
False, True, True, False)
End Class
Private Class KeyCombinationWithOffset
Public ReadOnly KeyCombination As KeyCombination
Public ReadOnly Offset As Point
Public Sub New(ByVal KeyCombination As KeyCombination, _
ByVal Offset As Point)
Me.KeyCombination = KeyCombination
Me.Offset = Offset
End Sub
Public Sub New(ByVal KeyCombination As KeyCombination, _
ByVal OffsetX As Integer, ByVal OffsetY As Integer)
MyClass.New(KeyCombination, New Point(OffsetX, OffsetY))
End Sub
End Class
Private f_KeyCombination As New KeyCombination
Private Shared ReadOnly AllCombinations As KeyCombinationWithOffset() _
= {New KeyCombinationWithOffset(KeyCombinations.Left, -1, 0), _
New KeyCombinationWithOffset(KeyCombinations.Right, 1, 0), _
New KeyCombinationWithOffset(KeyCombinations.Up, 0, -1), _
New KeyCombinationWithOffset(KeyCombinations.Down, 0, 1), _
New KeyCombinationWithOffset(KeyCombinations.LeftUp, -1, -1), _
New KeyCombinationWithOffset(KeyCombinations.LeftDown, -1, 1), _
New KeyCombinationWithOffset(KeyCombinations.RightUp, 1, -1), _
New KeyCombinationWithOffset(KeyCombinations.RightDown , 1, 1) _
}
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
For i As Integer = 0 To AllCombinations.Length - 1
Dim combi As KeyCombinationWithOffset = AllCombinations(i)
If combi.KeyCombination.Equals(f_KeyCombination) Then
Dim p As Point = Me.PictureBox1.Location()
p.X += combi.Offset.X
p.Y += combi.Offset.Y
Me.PictureBox1.Location = p
Exit For
End If
Next
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles MyBase.KeyDown
Dim EnableTimer As Boolean = True
Select Case e.KeyCode
Case Keys.Left
f_KeyCombination.Left = True
Case Keys.Right
f_KeyCombination.Right = True
Case Keys.Up
f_KeyCombination.Up = True
Case Keys.Down
f_KeyCombination.Down = True
Case Else
EnableTimer = False
End Select
If EnableTimer Then Timer1.Enabled = True
End Sub
Private Sub Form1_KeyUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles MyBase.KeyUp
Select Case e.KeyCode
Case Keys.Left
f_KeyCombination.Left = False
Case Keys.Right
f_KeyCombination.Right = False
Case Keys.Up
f_KeyCombination.Up = False
Case Keys.Down
f_KeyCombination.Down = False
End Select
If f_KeyCombination.Equals(KeyCombinations.None) Then
Timer1.Enabled = False
End If
End Sub
Armin