473,811 Members | 4,041 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4844
"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?

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" <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

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 GetAsyncKeyStat e Lib "user32" _
(ByVal vKey As Integer) As Integer

Private Sub Form1_KeyDown(B yVal sender As Object, ByVal e As
System.Windows. Forms.KeyEventA rgs) Handles Me.KeyDown

If CBool(GetAsyncK eyState(Keys.Up ) And &H8000) AndAlso
CBool(GetAsyncK eyState(Keys.Le ft) And &H8000) Then
Debug.WriteLine ("Up and Left")
End If

If CBool(GetAsyncK eyState(Keys.Up ) And &H8000) AndAlso
CBool(GetAsyncK eyState(Keys.Ri ght) And &H8000) Then
Debug.WriteLine ("Up and Right")
End If

If CBool(GetAsyncK eyState(Keys.Do wn) And &H8000) AndAlso
CBool(GetAsyncK eyState(Keys.Le ft) And &H8000) Then
Debug.WriteLine ("Down and Left")
End If

If CBool(GetAsyncK eyState(Keys.Do wn) And &H8000) AndAlso
CBool(GetAsyncK eyState(Keys.Ri ght) And &H8000) Then
Debug.WriteLine ("Down and Right")
End If

End Sub

"iwdu15" <jmmgoalsteraty ahoodotcom> wrote in message
news:9A******** *************** ***********@mic rosoft.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" <jmmgoalsteraty ahoodotcom> 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 GetAsyncKeystat e - 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
4515
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 which are fired when the cursor keys are pressed. I.e. pressing and releasing say, K, results in one keydown event followed by one keyup event. Press any of the cursor keys results in two keydown events followed by two keyup events. A page...
5
8031
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 for setting the hotkey. My problem is that WIN API takes a Keys object/enumeration. I'm a bit weak on that stuff, so I'm hoping someone can point me in the right direction? I'm looking for a good link or just out right help, please? If I...
2
2298
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 Ctrl-LeftMouseButton, etc. This could be intercepted by the form that owns the panel, but that does not seem very elegant. Are there any alternatives? I could use another type of base control if necessary. I'm mostly painting graphics.
3
5261
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 handler does not see the combination. Now this control is contained within another control which is contained within another. The top most control does see the CTRL+Z. I can easily pass down the key info, but why does the nested control see...
1
10157
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 Key. The code below shows what I mean. How can I cure this? (excuse me for the line breaks) */
4
3295
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, the control is meant to be a simple drawing program. To test it out I have put the control on a Windows form which also contains several buttons. I have added a KeyDown event handler to the custom control (see code below). It responds fine to keys...
1
3710
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 can capture a pressed key? If it possible, How to capture the screen (Screen picture)? Thanks for anyhelp.
3
5258
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 (like CTRL+ALT+SHIFT). TO this point I have code that only displays the context menu while keys CTRL +ALT+SHIFT are pressed - but after the keys are released the context menu still gets displayed. Can someone tell me where the following code is...
2
3103
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
9734
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10397
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10413
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10138
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5565
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5700
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3879
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.