"iwdu15" <jmmgoalsteraty ahoodotcom> 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 KeyCombinationW ithOffset
Public ReadOnly KeyCombination As KeyCombination
Public ReadOnly Offset As Point
Public Sub New(ByVal KeyCombination As KeyCombination, _
ByVal Offset As Point)
Me.KeyCombinati on = KeyCombination
Me.Offset = Offset
End Sub
Public Sub New(ByVal KeyCombination As KeyCombination, _
ByVal OffsetX As Integer, ByVal OffsetY As Integer)
MyClass.New(Key Combination, New Point(OffsetX, OffsetY))
End Sub
End Class
Private f_KeyCombinatio n As New KeyCombination
Private Shared ReadOnly AllCombinations As KeyCombinationW ithOffset() _
= {New KeyCombinationW ithOffset(KeyCo mbinations.Left , -1, 0), _
New KeyCombinationW ithOffset(KeyCo mbinations.Righ t, 1, 0), _
New KeyCombinationW ithOffset(KeyCo mbinations.Up, 0, -1), _
New KeyCombinationW ithOffset(KeyCo mbinations.Down , 0, 1), _
New KeyCombinationW ithOffset(KeyCo mbinations.Left Up, -1, -1), _
New KeyCombinationW ithOffset(KeyCo mbinations.Left Down, -1, 1), _
New KeyCombinationW ithOffset(KeyCo mbinations.Righ tUp, 1, -1), _
New KeyCombinationW ithOffset(KeyCo mbinations.Righ tDown, 1, 1) _
}
Private Sub Timer1_Tick(ByV al sender As System.Object, _
ByVal e As System.EventArg s) Handles Timer1.Tick
For i As Integer = 0 To AllCombinations .Length - 1
Dim combi As KeyCombinationW ithOffset = AllCombinations (i)
If combi.KeyCombin ation.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(B yVal sender As Object, _
ByVal e As System.Windows. Forms.KeyEventA rgs) _
Handles MyBase.KeyDown
Dim EnableTimer As Boolean = True
Select Case e.KeyCode
Case Keys.Left
f_KeyCombinatio n.Left = True
Case Keys.Right
f_KeyCombinatio n.Right = True
Case Keys.Up
f_KeyCombinatio n.Up = True
Case Keys.Down
f_KeyCombinatio n.Down = True
Case Else
EnableTimer = False
End Select
If EnableTimer Then Timer1.Enabled = True
End Sub
Private Sub Form1_KeyUp(ByV al sender As Object, _
ByVal e As System.Windows. Forms.KeyEventA rgs) _
Handles MyBase.KeyUp
Select Case e.KeyCode
Case Keys.Left
f_KeyCombinatio n.Left = False
Case Keys.Right
f_KeyCombinatio n.Right = False
Case Keys.Up
f_KeyCombinatio n.Up = False
Case Keys.Down
f_KeyCombinatio n.Down = False
End Select
If f_KeyCombinatio n.Equals(KeyCom binations.None) Then
Timer1.Enabled = False
End If
End Sub
Armin