2 keys pressed at once

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?

thanks
 
iwdu15 said:
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?


I think, you don't want to move them due to 'keypress'es but due to holding
down the keys. There's no keys-held-down-event, or something like this, thus
you'd have to use a timer to move the picbox. Use the keydown event to set
the flags which keys are held down and in timer.tick, look at the flags and
move accordingly.


Armin
 
iwdu15 said:
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
 
i tried that and it didnt do anything, and i prob did it wrong, but its hard
to fix when i dont understand the code. It looks to me like the keydown event
only captures one key pushed at once, making what i want to do
impossible...so im wondering if theres a different way? for instance API
calls or a keyboard hook? im just throwing things out there, but let me give
a brief overview on what im doing to maybe clarify a bit.

im creating a simulator for my FIRST team. simple 2d graphics to work on
tactics. what i need to be able to move a picturebox around the screen
fluently in all directions. hope this helps out at all
 
Give this a try.

Public Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Integer) As Integer

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

If CBool(GetAsyncKeyState(Keys.Up) And &H8000) AndAlso
CBool(GetAsyncKeyState(Keys.Left) And &H8000) Then
Debug.WriteLine("Up and Left")
End If

If CBool(GetAsyncKeyState(Keys.Up) And &H8000) AndAlso
CBool(GetAsyncKeyState(Keys.Right) And &H8000) Then
Debug.WriteLine("Up and Right")
End If

If CBool(GetAsyncKeyState(Keys.Down) And &H8000) AndAlso
CBool(GetAsyncKeyState(Keys.Left) And &H8000) Then
Debug.WriteLine("Down and Left")
End If

If CBool(GetAsyncKeyState(Keys.Down) And &H8000) AndAlso
CBool(GetAsyncKeyState(Keys.Right) And &H8000) Then
Debug.WriteLine("Down and Right")
End If

End Sub
 
iwdu15 said:
i tried that and it didnt do anything, and i prob did it wrong, but
its hard to fix when i dont understand the code. It looks to me like
the keydown event only captures one key pushed at once, making what
i want to do
impossible...

Yes, keydown captures only one key, but that's why I wrote the code.
so im wondering if theres a different way?

Yes, you can use GetAsyncKeystate - as suggested by Rocky meanwhile - but
than you have to check the keystate each time and don't have an up-to-date
object. However, both is possible.
for instance
API calls or a keyboard hook?
im just throwing things out there, but
let me give a brief overview on what im doing to maybe clarify a
bit.

im creating a simulator for my FIRST team. simple 2d graphics to
work on tactics. what i need to be able to move a picturebox around
the screen fluently in all directions. hope this helps out at all

That's what the code does.


If you want real-time animations, controls are not the way to go. In this
case, the whole code should be put in a main loop,
a) updating key states on key messages
b) doing animations depending on the current key states
c) rendering the display.

That's too much for me to explain here. Thus, first, we should examine why
my code does not work. Here, it does what you expect. The code captures the
Form's key events. If there are controls that can get the focus, you would
have to set the Forms's KeyPreview property to True. Have you tried putting
the code into a new project (including a picbox and a Timer)?



Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top