Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vbkey help...

Status
Not open for further replies.

JaXx

Programmer
Dec 23, 2000
1
GB
i'm new to vb and i use vb 4 so don't give me any vb5 + stuff please. i'm trying to make a basic lander game so that you can just land a oval shape (ship) onto another rectangle shape (land). i want to use the vbkey thing so i can use the keyboard to control the ship. all i know is to set the form's keypreview to true and that's it. after that, what do i do to tell the program that when i press the up arrow on my keyboard the ship moves up, say 5 pixels.
thanks for reading and i hope to get a few replys and stuff.
JaXx - email@jgnx.net
 
Did you try reading the help file? Start VB4, load your program, view code, click on help, click search, type in keyup.

You'll probably have to create a PUBLIC SUB in the main module (if that's what you're using). Otherwise, you'll want to create a Class Module for all your programs that use that/those particular key stroke sequences.

--MiggyD
 
Well surly all you need to do is identify the key, then say pic.top = pic.top - 5 ? Ok so your gonna need to change the scalemode of your form first, (to pixles) and i would reccomend you use a picture for your ship other wise you will have to erase the last shape that you printed last time.

Rear Chud
x5095@junkie.com
 
Got this from the net awhile back, If I remember right I had to tweak it a bit, but don't remember what I did. You could also search the help file for VB CONSTANTS to get all the other keys.

Sub Form_KeyDown(keycode As Integer, Shift As Integer)
If keycode = 39 Then
' right arrow key was pressed
Picture1.Move Picture1.Left - 100
End If

If keycode = 37 Then
' left arrow key was pressed
Picture1.Move Picture1.Left + 100
End If

If keycode = 38 Then
' up arrow key was pressed
Picture1.Move Picture1.Top + 100
End If

If keycode = 40 Then
' down arrow key was pressed
Picture1.Move Picture1.Top - 100
End If
End Sub

Dragnut
 
Well...
dragnuts code looks allright, although I would suggest replacing the 4 If-statements by one Case-statement.

Sub Form_KeyDown(keycode As Integer, Shift As Integer)

Select Case keycode

case 39
Picture1.Move Picture1.Left - 100

case 37
Picture1.Move Picture1.Left + 100

case 38
Picture1.Move Picture1.Top + 100

case 40
Picture1.Move Picture1.Top - 100

end select

End Sub

Have Phun and remember Brad Nowell...
-Stylee

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top