February 27, 2021
Hot Topics:

Using Graphics: Making a Lander Game

  • By John Percival
  • Send Email »
  • More Articles »

Since we will be using a timer to do the drawing and moving of the craft, it would seem sensible to also check to see whether any thrust to slow the craft has been used, and change the speed appropriately. Since there is no VB command to check whether a key is pressed down, we must look to the API, and the GetAsyncKeyState function. Basically, this function returns 0 if the specified key is not pressed and has not been pressed since the previous call of the function, otherwise it will return a non-zero value.

The required declares are:

Private Declare Function GetAsyncKeyState _
  Lib "user32" (ByVal vKey As Long) As Integer
Private Const VK_DOWN = &H28

In the vKey parameter of the function, a number is passed referring to a certain key code, called the 'Virtual Key Code'. There is a code for every key on the keyboard, including ones to differentiate between left and right shifts, ctrls and alts. All we will be using is the down key, as a thruster.

Here is a snippet of code that we could use:

If GetAsyncKeyState(VK_DOWN) <> 0 Then
  ' Do thrust stuff
End If

Although that particular piece of code will not do much, it gives the general idea.

Page 5 of 8

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date