Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

VB6 Detect Keypress

I was wondering what the code is to detect a keypress. I need it to start practicing making games using VB6.

Comments

  • MihailVDMihailVD Posts: 91Member
    : I was wondering what the code is to detect a keypress. I need it to start practicing making games using VB6.
    :
    Interesting question.
    I think I can help you on this one.
    The first, easy, but not so good-resulted method is to use the events of your Visual Basic project form:
    ---
    Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyUp Then MovePlayerUp
    If KeyAscii = vbKeyDown Then MovePlayerDown
    If KeyAscii = vbKeyLeft Then MovePlayerLeft
    If KeyAscii = vbKeyRight Then MovePlayerRight
    End Sub
    ---
    And if you want to detect when the user RELEASES the key:
    ---
    Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    'Code Here
    End Sub
    ---
    The bad thing on using this method is that you cannot detect two keys
    pressed at a time.

    Now...the really good method, the second method.
    Put this in a module to define the key pressing function of the WinAPI:
    ---
    Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    ---
    And then, when you want to detect if a key is pressed:
    ---
    If GetAsyncKeyState(vbKeyUp) < 0 Then MovePlayerUp
    If GetAsyncKeyState(vbKeyDown) < 0 Then MovePlayerDown
    If GetAsyncKeyState(vbKeyLeft) < 0 Then MovePlayerLeft
    If GetAsyncKeyState(vbKeyRight) < 0 Then MovePlayerRight
    ---

    Good Luck!
    ---------------
    Mihail Dimitrov
    ICQ : 110064098

  • KDivad LeahcimKDivad Leahcim Posts: 3,948Member
    : : I was wondering what the code is to detect a keypress. I need it to start practicing making games using VB6.
    : :
    : Interesting question.
    : I think I can help you on this one.
    : The first, easy, but not so good-resulted method is to use the events of your Visual Basic project form:
    : ---
    : Private Sub Form_KeyPress(KeyAscii As Integer)
    : If KeyAscii = vbKeyUp Then MovePlayerUp
    : If KeyAscii = vbKeyDown Then MovePlayerDown
    : If KeyAscii = vbKeyLeft Then MovePlayerLeft
    : If KeyAscii = vbKeyRight Then MovePlayerRight
    : End Sub
    : ---
    : And if you want to detect when the user RELEASES the key:
    : ---
    : Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    : 'Code Here
    : End Sub
    : ---
    : The bad thing on using this method is that you cannot detect two keys
    : pressed at a time.
    :
    : Now...the really good method, the second method.
    : Put this in a module to define the key pressing function of the WinAPI:
    : ---
    : Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    : ---
    : And then, when you want to detect if a key is pressed:
    : ---
    : If GetAsyncKeyState(vbKeyUp) < 0 Then MovePlayerUp
    : If GetAsyncKeyState(vbKeyDown) < 0 Then MovePlayerDown
    : If GetAsyncKeyState(vbKeyLeft) < 0 Then MovePlayerLeft
    : If GetAsyncKeyState(vbKeyRight) < 0 Then MovePlayerRight
    : ---
    :
    : Good Luck!
    : ---------------
    : Mihail Dimitrov
    : ICQ : 110064098
    :
    :


    Two cents:

    KeyDown, not KeyPress. And you can use KeyDown for multiple keys. In fact, you can do so easier and more reliably than with GetAsyncKeyState because the API needs a loop or timer.
  • rubierubie Posts: 1Member
    With timer is better, because if the computers gets faster, the control will react stronger. If you have a timer, this is no problem
Sign In or Register to comment.