Results 1 to 3 of 3

Thread: Arrow Keys

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2001
    Location
    na
    Posts
    2

    Arrow Keys

    How can I get seemless input from the keyboard. If I try to use the arrow keys to move and object it usually stops, and also I want to be able to use more than one key in conjunction. Like up and left will move the control upward and left and the same time at a speed set in a variable. I prefer you email me, because I don't check the forum often, but any answer is appreciated.

  2. #2
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Two things:

    You need a main loop that calls a CheckKeys function each time.

    And you need a array that contains each key--

    like this:

    Code:
    'Declares
        Dim KD(255) As Boolean
    
    Sub CheckKeys()
        If KD(vbKeyUp) Then: Picture.Top = Picture.Top - 1
        If KD(vbKeyRight) Then: Picture.Left = Picture.Left + 1
        If KD(vbKeyDown) Then: Picture.Top = Picture.Top + 1
        If KD(vbKeyLeft) Then: Picture.Left = Picture.Left - 1
    End Sub
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        KD(KeyCode) = True
    End Sub
    
    Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
        KD(KeyCode) = False
    End Sub

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    And if you dont know how to have it called every now and again, try something like this :

    Code:
    Option Explicit
    Private DoLoop As Boolean
    Private CurrentTick As Long
    Private KD(255) As Boolean
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    
    Private Sub Form_Load()
        Me.Show
        Me.KeyPreview = True
        DoLoop = True
        RunGameLoop
    End Sub
    
    Private Sub RunGameLoop()
        Do While DoLoop
        DoEvents
            If ((GetTickCount() - CurrentTick) > 50) Then
                CurrentTick = GetTickCount()
                CheckKeys
            End If
        Loop
    End Sub
    
    Sub CheckKeys()
        If KD(vbKeyUp) Then Picture1.Top = Picture1.Top - 10
        If KD(vbKeyRight) Then Picture1.Left = Picture1.Left + 10
        If KD(vbKeyDown) Then Picture1.Top = Picture1.Top + 10
        If KD(vbKeyLeft) Then Picture1.Left = Picture1.Left - 10
    End Sub
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        KD(KeyCode) = True
    End Sub
    
    Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
        KD(KeyCode) = False
    End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

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