VB6 Detect Keypress - Programmers Heaven

Howdy, Stranger!

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

Categories

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.