what code would i need to get a keypress in side of a timer?
Printable View
what code would i need to get a keypress in side of a timer?
Do you mind explaining a bit more ? :)
:wave:
i'm trying to program a platfrom game for my major project, which i have to code in vb, i've programmed a platfrom game before in C, i'm using a timer to emulate the sequential programming the i am use to in C, so i don't have to change code much , but i'm having trouble with what code i need to get the "keypress" function to work in the the timer so i can get my character to move.
i hope this makes sence :P
Maybe GetAsyncKeyState API would help you.
Example:
Try pressing both the right & down key. You'll see that it moves in a lean way to right.vb Code:
Option Explicit '~~~ API Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer '~~~ Start the Timer Private Sub Form_Load() Timer1.Interval = 1 Timer1.Enabled = True End Sub '~~~ We are trying to detect the key being pressed and do whatever we want Private Sub Timer1_Timer() '~~~ Move up If GetAsyncKeyState(vbKeyUp) Then Command1.Top = Command1.Top - 10 End If '~~~ Move down If GetAsyncKeyState(vbKeyDown) Then Command1.Top = Command1.Top + 10 End If '~~~ Move left If GetAsyncKeyState(vbKeyLeft) Then Command1.Left = Command1.Left - 10 End If '~~~ Move right If GetAsyncKeyState(vbKeyRight) Then Command1.Left = Command1.Left + 10 End If End Sub
:wave:
thank you it works.