using arrow keys - Techist - Tech Forum

Go Back   Techist - Tech Forum > Computer Software > Programming
Click Here to Login
Closed Thread
 
Thread Tools Display Modes
 
Old 08-22-2004, 01:56 AM   #1 (permalink)
True Techie
 
Join Date: Jun 2004
Posts: 155
Default using arrow keys

I am making a 'space invaders' look a like. i need to move the little ship using the arrow keys and i dont know how. if there is no way than can someone direct me as to how to move the ship left or right with some other keys. thanks
__________________

wannabe_hacker is offline  
Old 08-23-2004, 04:15 AM   #2 (permalink)
Banned
 
Join Date: Aug 2004
Posts: 50
Default

if you have made the space invaders game in visual basic then to use the arrow keys as far as i know then u would just need to find out when the user presses the relevant arrow key. To do that you would use the following to find out which key was pressed:

on the control you are using on the key press event or something similiar there should be a keyascii event.

If you make a Textbox1.Text =keyascii

and then press one arrow key at a time it will tell you the keyascii that you have to look out for, so if they press the left arrow key then you have to do a select case to detect which key ascii was pressed and then do what ever you want to do once u have detected which key was pressed. I hope that is what you were looking for.

If not let me know exactly what you want!
__________________

gecko_au2003 is offline  
Old 08-23-2004, 04:19 AM   #3 (permalink)
Banned
 
Join Date: Aug 2004
Posts: 50
Default

If it is ok , can you send me the souce code for this game you are making ??
gecko_au2003 is offline  
Old 08-24-2004, 12:38 AM   #4 (permalink)
True Techie
 
Join Date: Jun 2004
Posts: 155
Default

I have no source code as of now. I just want to know how to move the ship from left to right. I want to put that code in first so I can move the ship, if I can't move the ship then there is really no point of the game.
wannabe_hacker is offline  
Old 08-24-2004, 05:17 AM   #5 (permalink)
Super Techie
 
Join Date: Jun 2004
Posts: 309
Send a message via Yahoo to sippin codeine
Default

I dont really use VB anymore but this might help..
Code:
Option Explicit
Private Const VK_LEFT = &H25
Private Const VK_RIGHT = &H27
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

Private Sub Form_Load()
    If GetKeyState(VK_LEFT) < 0 Or GetKeyState(VK_RIGHT) < 0 Then
       do something
    Else
       End Sub
    End If
End Sub
__________________
**[System specs]**

Delphi Enterprise 6 - 7
VB 6.0 - 2005 EE
sippin codeine is offline  
Old 08-24-2004, 11:09 PM   #6 (permalink)
Banned
 
Join Date: Aug 2004
Posts: 50
Default

here I can do an example now cos I am not sure of the keys ascii code. Then I will post it here, I am assuming you will use a picturebox or an image for the ship so you can load an image of the ship to use as the ship , right ?
gecko_au2003 is offline  
Old 08-24-2004, 11:30 PM   #7 (permalink)
Banned
 
Join Date: Aug 2004
Posts: 50
Default

that function that sippine codine or w.e his id is posted looks good but does that only work with the left and right arrow keys ? If so, what about up and down ?
gecko_au2003 is offline  
Old 08-24-2004, 11:33 PM   #8 (permalink)
Banned
 
Join Date: Aug 2004
Posts: 50
Default

ok the keyascii does not work with the arrow keys, i will have a look at the function that was posted earlier
gecko_au2003 is offline  
Old 08-24-2004, 11:45 PM   #9 (permalink)
Banned
 
Join Date: Aug 2004
Posts: 50
Default

Copy and paste the code below in to your visual basic project. Then make a label called lblkeys on your form and run it :P

I was wrong earlier on because I didnt know that the keyascii function variable didnt get or capture the arrow keys !!

=============================================

'This vector contains the state of all keys
'I think KeyCode goes from 0 to 255, but you
'can change this if you want
Dim Keys(255) As Boolean

'Its used to stop the Do-Loop of the Form_Load
Dim StopLoop As Boolean

Private Sub Form_KeyDown(KeyCode As Integer, Shift As _
Integer)
'The key KeyCode is pressed now...
Keys(KeyCode) = True
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
'The key KeyCode is NOT pressed now...
Keys(KeyCode) = False
End Sub

Private Sub Form_Load()
Dim str As String

Me.Show

'The form must handle the key events
Me.KeyPreview = True

StopLoop = False

Do
'Just check for the arrow keys to see
'if they are pressed. To check form more
'keys, just ask for the correct subindex
'in the "Keys" array
str = ""

If Keys(vbKeyUp) Then str = str + "UP" & vbCrLf
If Keys(vbKeyDown) Then str = str + "DOWN" & vbCrLf
If Keys(vbKeyLeft) Then str = str + "LEFT" & vbCrLf
If Keys(vbKeyRight) Then str = str + "RIGHT" & vbCrLf

'Refresh the label
lblkeys.Caption = str

'Important!! (to not freeze the thing)
DoEvents
Loop Until StopLoop
End Sub

Private Sub Form_Unload(Cancel As Integer)
'Important!! To stop the loop, so the
'form can be unloaded
StopLoop = True
End Sub
gecko_au2003 is offline  
Old 08-26-2004, 09:19 PM   #10 (permalink)
Banned
 
Join Date: Aug 2004
Posts: 50
Default

once you insert that code you just insert a picturebox or an image and insert this code.

Private Sub Picture1_KeyDown(KeyCode As Integer, Shift As Integer)
If Keys(vbKeyUp) Then Picture1.Top = Picture1.Top - 100
If Keys(vbKeyDown) Then Picture1.Top = Picture1.Top + 100
If Keys(vbKeyLeft) Then Picture1.Left = Picture1.Left - 100
If Keys(vbKeyRight) Then Picture1.Left = Picture1.Left + 100
End Sub

if you insert an image instead of a picturebox then obviously you will need to alter the code so it is image1.left or image1.top, etc.

Obviously you will need to change the coding if you change the name of the control.
__________________

gecko_au2003 is offline  
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Copyright 2002- Social Knowledge, LLC All Rights Reserved.

All times are GMT -5. The time now is 03:28 AM.


Powered by vBulletin® Version 3.8.8 Beta 1
Copyright ©2000 - 2017, vBulletin Solutions, Inc.