Advertisement Jump to content
Sign in to follow this  

VB: Keyboard problem

This topic is 5001 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im programming a game in VisualBasic. The problem: To move around you use the keyboard. However lets say if youre moving right and then jump up the chara stops moving right even though you are still holding the right key down. This includes what I got so far in the game and if you try it out you can see the problem. Also I got the code that i use for the keys. I dont think any other code is necessary. Use arrow keys for Luigi and W-D-A for toad. Anyone know how this can be fixed? >< Thanks in advance.

Share this post

Link to post
Share on other sites
Well, this is the first VB code I've ever seen. It looks like a switch statement. That won't work. That's like saying this:

if left then move-left
else if right then move-right
else if up then jump

When you need this:

if left then move-left
if right then move-right
if up then jump

However, to get a more desirable effect, you should make the character keep moving at the same speed he was moving when he jumped. So if the right key was down when he pressed jump, he keeps moving right while he's in the air, regardless of the right key's state.

I hope that helps. I can't help with code specifics.

Share this post

Link to post
Share on other sites
Your problem can be explained simply:

You're pressing the right button, and when you do so, it will trigger a OnKeydown (or something like that). However, when you press space, it will trigger ANOTHER OnKeydown. The previous key(The movement) is still down, so it won't trigger a keydown event.

Your solution is not to rely on the events for this, but to use the GetAsynchKeyState function. This function will ask Windows about the state of a specific key. So, if the key is down, Windows will tell you so.

Declare this in the file where you check for the key down:

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

Now, in order to check if a key is pressed, use this code:

Private Function KeyDown(key As Integer) As Boolean
KeyDown = (GetAsyncKeyState(vbKeyW) And &H8000)
End Function

if KeyDown(vbKeyW) then
' OMG, the W key is pressed at the moment, do something!
end if

if KeyDown(vbKeyA) then
' You get the idea I guess
end if

To get a complete list of avaliable keys you can get:

Sorry if I made code errors, I'm rusty on Visual Basic since I programmed in VB at least 2 years ago.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!