PDA

Click to See Complete Forum and Search --> : Arrow Keys


Beezelbub
Apr 29th, 2001, 03:28 PM
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.

Fox
Apr 29th, 2001, 11:57 PM
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:


'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

plenderj
Apr 30th, 2001, 02:06 AM
And if you dont know how to have it called every now and again, try something like this :


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