Results 1 to 11 of 11

Thread: [RESOLVED] [2005] Detect Multiple Keypresses

  1. #1

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Resolved [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?
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    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.

  3. #3

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    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.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  4. #4
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    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.

  5. #5

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    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.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  6. #6
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    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.

  7. #7
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    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

  8. #8
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    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.

  9. #9

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    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.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  10. #10
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    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.

  11. #11

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    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.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width