Results 1 to 6 of 6

Thread: KeyPress events - any way to press keys simultaneously?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2000
    Posts
    103
    Private Sub picShip_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
    Case Asc("n") 'left
    If picShip.Left > 75 Then
    picShip.Move (picShip.Left - 75)
    End If
    Case Asc("m") 'right
    If (picShip.Left + picShip.Width) < (frmMain.Width - 75) Then
    picShip.Move (picShip.Left + 75)
    End If
    Case Asc("z") 'shoot
    shpBullet.Move (picShip.Left + (picShip.Width / 2)), picShip.Top
    shpBullet.Visible = True
    Dim shot As clsShootBullet
    Set shot = New clsShootBullet

    End Select
    End Sub

    I want to be able to shoot and move left at the same time for example.

  2. #2
    Guest

    KB Handler

    You need to write your own keyboard handler routine to make this work. What you want to do is use the keyPreview property to handle keystrokes at the form level. Since no two keystrokes ever occur at exactly the same time, you need to allow a buffer arount the key pressed event. Instead of considering a key pressed at an instant, consider it as an instant plus 50 milliseconds. If another key is hit in that time, then consider the two instantaneous and execute a moving&shooting function.

    Hope this helps, let me know if you need more info

    -John

  3. #3
    Junior Member
    Join Date
    Feb 2000
    Posts
    21
    Altough I do not think you can detect if twos key are pressed at exactly the same time, you can detect if two keys are pressed simultaneously by setting variables and testing them in a timer. I used labels in the following example to illustrate the key status. You need three labels, one timer and one shape in your form to put it to work. Simple, you will see.

    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
      Select Case KeyCode
        Case Asc("A")
          Label1.Caption = "a"
        Case Asc("S")
          Label2.Caption = "s"
        Case Asc("D")
          Label3.Caption = "d"
      End Select
    End Sub
    
    Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
      Select Case KeyCode
        Case Asc("A")
          Label1.Caption = ""
        Case Asc("S")
          Label2.Caption = ""
        Case Asc("D")
          Label3.Caption = ""
      End Select
    End Sub
    
    Private Sub Timer1_Timer()
    
      Shape1.Top = Shape1.Top + 75
    
      If Label1 <> "" Then
        Shape1.Left = Shape1.Left - 75
      End If
    
      If Label2 <> "" Then
        Shape1.BackColor = vbBlue
      Else
        Shape1.BackColor = vbRed
      End If
    
      If Label3 <> "" Then
        Shape1.Left = Shape1.Left + 75
      End If
    
      If Shape1.Top > ScaleHeight Then Shape1.Top = 0
    End Sub

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I think you should use GetAsyncKeyState API to check which button is pressed if you have more than one button pressed simulateously.

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


    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2000
    Posts
    103
    If one button is pressed and another immediately after, the 1st button is cut off (stops executing) and the second one starts executing. I want to be able to have the first one keep executing. There has to be a way to do this. Think of the 3D games that use the arrow keys and diagonal movement.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Thats odd, this was lost from my previous post:

    Code:
    If GetAsyncKeyState(vbKeyF1) And GetAsyncKeyState(vbKeyF2) Then Msgbox "F1 and F2 was pressed"
    This works always
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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