by Joshua Conklin (3 Submissions)
Category: Coding Standards
Compatability: Visual Basic 5.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (2 Votes)
This article is trying to help show how to program in visual basic, how to make applications execute commands upon the keystroke of a specific key.
Have you ever wondered how to make your application execute a unique command when a specific key has been pressed?
Say you press the 'w' key, you could make your program say "Hey! don't touch my w key on the keyboard its mine!".
Or knowing this you can create games. You can be allowed to use the arrow keys for movement of characters or make the spacebar shoot or punch or whatever.
First off create a new project and start with your Form1..
double click on your form and get to the form1 code area. from here, select form1 in the item combo box and then in the second combo box on the right, select your form key down event.
so right now you should see this:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
End Sub
now for some easy stuff... lets do what i said in the above situation. Lets make our app say "dont touch my letter W!!"
all you have to do is insert this code between your form_keydown event:
If KeyCode = vbKeyW Then MsgBox "Dont touch my letter W!!"
Taa Daa!! success eh? It always feels good when the W no touch test is a success! :D
Now you have a basic idea!
Now with this knowledge we can start doing some tricky stuff...
you could add a shape like a circle and move it left or right... want to know how? I bet you do!
so add the Circle and name it 'theCircle'
this is where some of your own personal programmign skills would come on.. i am sure there are hundreds of ways to make movements and stuff, but the way i do it is this... I create individual Private Subs for each different movement or event that i want my circle to do... and hten i put my code in it. I will show you below and you can figure it out:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyLeft Then CircleGoLeft
If KeyCode = vbKeyRight Then CircleGoright
End Sub
Private Sub CircleGoLeft()
thecircle.Left = thecircle.Left - 100
End Sub
Private Sub CircleGoright()
thecircle.Left = thecircle.Left + 100
End Sub
OOOO!!! awesome huh? just think now! The possibilities are endless! Now you are on your way to gaming in no time! Drop me a line if you think my tut' really stinks or you have an idea, or want to tell me that you just sold a million copies of your game and that i was the one who taught you form_keydown event :-)
Happy Programming!
* Love Peace & Chicken Grease *
- Josh Conklin