473,326 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

2 keys pressed at once

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
Jan 8 '06 #1
5 4776
"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?

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

Jan 8 '06 #2
"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

Jan 8 '06 #3
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
--
-iwdu15
Jan 8 '06 #4
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" <jmmgoalsteratyahoodotcom> wrote in message
news:9A**********************************@microsof t.com...
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
--
-iwdu15

Jan 8 '06 #5
"iwdu15" <jmmgoalsteratyahoodotcom> schrieb
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

Jan 8 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: inevercheckthisaddress | last post by:
I'm writing some stuff where I wish to allow the cursor keys to control elements in a page. This has not been a problem except with Safari which appears to duplicate the keydown and keyup events...
5
by: Flip | last post by:
I'm trying to create a form which a user can type out a key, and the CTRL-ALT-SHIFT keys are recognized (via ModifiedKeys object) and then pass the letter the user pressed to the windows API method...
2
by: _DD | last post by:
I'm trying to gracefully intercept Control and Alt keys for mouse events within a Panel. I realize that keyboard is not normally relevant there, but in this case, I need to get...
3
by: MLM450 | last post by:
I have a control that handles the KeyDown event but it does not seem to execute when a combination of keys is pressed - like CTRL+Z. If I press CTRL, it executes. If I press Z, it executes. But the...
1
by: Martijn Mulder | last post by:
/* I have problems detecting the Arrow Keys on a User Control. A control derived from System.Windows.Forms.Control neglects 'bare' Arrow Keys but does react on the combination <Altor <Ctrl+ Arrow...
4
by: boopsboops | last post by:
Hi thescripts people, I hope I'm in the right forum for Visual Basic Dotnet (VS 2005). I am trying to make a custom control in which you can nudge a point around using the arrow keys. Actually,...
1
Atran
by: Atran | last post by:
Hello, When a key pressed on keyboard, I want my program capture the pressed key, and save the pressed keys in a notepad. You listen about monitor programs, that programs capture keys........ How I...
3
by: hzgt9b | last post by:
Using VS2005, VB.NET, I am developing a windows app that has a DataGridView. I want to enable the display of a context menu on this DataGridView only when a specific set of keys is also pressed...
2
by: John | last post by:
Hi How can I capture control keys Ctrl-A, Ctrl-B, Ctrl-C etc. in a form? Thanks Regards
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.