|
-
Mar 4th, 2009, 06:04 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] [2005] Detect Multiple Keypresses
I'm making a mario-like clone and I just finished the jumping code. The only problem is that moving while jumping is extremely hard because if you hold down the up key and right key at the same time, neither one activates. Is there a Key Code or something that I can use to detect when they are both pressed?
-
Mar 4th, 2009, 06:20 PM
#2
Re: [2005] Detect Multiple Keypresses
I find this works quite well.
Code:
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
LeftState = GetAsyncKeyState(Keys.Left)
UpState = GetAsyncKeyState(Keys.Up)
'etc
If leftstate AndAlso upstate Then TextBox1.Text = "Yes" Else TextBox1.Text = "No"
End Sub
Private Sub Form1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
LeftState = GetAsyncKeyState(Keys.Left)
UpState = GetAsyncKeyState(Keys.Up)
'etc
If leftstate AndAlso upstate Then TextBox1.Text = "Yes" Else TextBox1.Text = "No"
End Sub
Dont be tempted to do something like If GetAsyncKeyState(Keys.Left) And GetAsyncKeyState(Keys.Up) though, always put a line per key.
Last edited by Bulldog; Mar 4th, 2009 at 06:23 PM.
-
Mar 4th, 2009, 06:26 PM
#3
Thread Starter
Fanatic Member
Re: [2005] Detect Multiple Keypresses
Ok thanks, but why can't I put them on the same line? Also isn't "And" the same as "AndAlso", I never understood the difference.
-
Mar 4th, 2009, 06:27 PM
#4
Re: [2005] Detect Multiple Keypresses
I find this works quite well.
Code:
Private Declare Function GetAsyncKeyState Lib "user32" _
(ByVal keyCode As Integer) As Integer
Private Sub DetectKeys()
LeftState = GetAsyncKeyState(Keys.Left)
UpState = GetAsyncKeyState(Keys.Up)
If LeftState AndAlso UpState Then TextBox1.Text = "Yes" Else TextBox1.Text = "No"
End Sub
Private Sub Form1_KeyDown(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
DetectKeys()
End Sub
Private Sub Form1_KeyUp(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
DetectKeys()
End Sub
Dont be tempted to do something like If GetAsyncKeyState(Keys.Left) And GetAsyncKeyState(Keys.Up) though, always put a line per key.
Edit: Sorry just tidied it.
-
Mar 4th, 2009, 06:29 PM
#5
Thread Starter
Fanatic Member
Re: [2005] Detect Multiple Keypresses
Is there a way to do it without making it a separate function?
EDIT: Nvm, can you just answer 2nd post, thanks.
-
Mar 4th, 2009, 06:33 PM
#6
Re: [2005] Detect Multiple Keypresses
You can use the GetAsyncKeyState(Key....) outside of an event, this API just indicates the state of a key. So you could just use "If GetAsyncKeyState(Keys.Left) And GetAsyncKeyState(Keys.Up)" in the body of your code but it is only valid while both keys are down of course.
-
Mar 4th, 2009, 06:37 PM
#7
Re: [2005] Detect Multiple Keypresses
I dont know why being on the same line doesnt always work. Give it a go, it just seems a little jittery when its done like that. I just modified the code like this and seems to be fine. Im sure I had a problem in the past though
Code:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal keyCode As Integer) As Integer
Private Sub DetectKeys()
If GetAsyncKeyState(Keys.Left) And GetAsyncKeyState(Keys.Up) Then _
TextBox1.Text = "Yes" Else TextBox1.Text = "No"
End Sub
Private Sub Form1_KeyDown(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
DetectKeys()
End Sub
Private Sub Form1_KeyUp(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
DetectKeys()
End Sub
-
Mar 4th, 2009, 06:40 PM
#8
Re: [2005] Detect Multiple Keypresses
Ah, I think I remember, you cant do it on one line if its done in just one key event (up or down), if you use both key up/down events its fine.
-
Mar 4th, 2009, 07:45 PM
#9
Thread Starter
Fanatic Member
Re: [2005] Detect Multiple Keypresses
Ok thanks. Also I realize I don't really need a different action for when there both down, I just need a way to detect whether one is down or not when more than one is pressed. This should work thanks.
EDIT: Why is it that it only works without a conditional operator. How come = True or = False doesn't work? Sorry for the questions =P just trying to learn.
EDIT2: How can I do the False path or Not path of them, this works but when I try to make it shorter with Not or = False neither works.
Code:
If GetAsyncKeyState(Keys.Left) Or GetAsyncKeyState(Keys.Right) Then
'blah
Else
direction = 0
End If
I have to get direction to = 0 when neither of them is being pressed, I have this code in the key up event.
Last edited by Vectris; Mar 4th, 2009 at 08:09 PM.
-
Mar 4th, 2009, 08:02 PM
#10
Re: [RESOLVED] [2005] Detect Multiple Keypresses
That's the reason you need to put in the Up and Down event.
Code:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal keyCode As Integer) As Integer
Private Sub DetectKeys()
If GetAsyncKeyState(Keys.Left) And GetAsyncKeyState(Keys.Up) Then _
TextBox1.Text = "Yes" Else TextBox1.Text = "No"
End Sub
Private Sub Form1_KeyDown(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
DetectKeys()
End Sub
Private Sub Form1_KeyUp(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
DetectKeys()
End Sub
If you only use one event, key down for example, when both are down the event triggers, but if one is then taken up, the event does not fire again, so nothing will change. If you put it in up and down, then the status is evaluated in both states.
The result of the API is actually an integer rather than a boolean, its 0 or 1 which can be correctly interpreted as True or False, but the value returned is actually 0 or 1.
-
Mar 4th, 2009, 08:24 PM
#11
Thread Starter
Fanatic Member
Re: [RESOLVED] [2005] Detect Multiple Keypresses
Ok thanks. Also I knew about using the Key Up, it was at the end of that post. But what you said is still correct.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|