Click to See Complete Forum and Search --> : I need help with using the arrow keys
ToneDogg
May 19th, 2000, 04:28 AM
I am making a tetris game for my VB class in school and I wanted to use the right and left arrow keys to move the pieces, the down arrow key to drop the piece faster and the up arrow key to rotate the piece. I have 200 command buttons on the form which I use the backcolor to animate the pieces (I know I'm a newbie still shush :( ) and I have the form's keypreview set to true but it's not responding to the arrow keys keypresses. I know I have the correct ascii codes because I opened a new project and made message boxes appear when you pressed the corresponding arrow keys. I need help getting the arrow keys to call the functions and or sub routines to move the pieces please, this project is due soon... thank you in advance.
[Edited by ToneDogg on 05-19-2000 at 05:30 PM]
Try this code. This uses the GetAsyncKeyState API and it
will detect if the key is pressed even if the App doesn't have the focus.
First, put this code in your module
Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Long) As Integer
Next, put this code in a Timer on your Form. Set the Timer's
Interval property to 1.
Private Sub Timer1_Timer()
If GetAsyncKeyState(vbKeyLeft) Then ' Move piece left
If GetAsyncKeyState(vbKeyRight) Then ' Move piece right
If GetAsyncKeyState(vbKeyDown) Then ' Drop piece
End Sub
If you do not wish to use that method for any reason, you could still use the other method without API.
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = VbKeyLeft Then ' Move piece left
If KeyCode = VbKeyRight Then ' Move piece right
If KeyCode = VbKeyDown Then ' Move piece down
End Sub
ToneDogg
May 19th, 2000, 05:15 AM
Thanks. I tried the first one and that does work. However I already have a timer in the program which is used to drop the pieces and when I put a timer2 into it the pieces fly down like timer1 has an interval of 1. The second part of what you said is the code I wanted to use and thats exaclty what I had but it doesn't respond to key presses. :( Oh and also what you said about that api working even if the app doesn't have the focus. Could it be that since the command buttons are changing they have the focus even though I have their enabled property set to false? Maybe what I need to know is how to give the form the focus all the time, then I could use the second part of what you said using the Form_KeyDown event...
[Edited by ToneDogg on 05-19-2000 at 06:19 PM]
The API detects whenever a certian Key is pressed whenever the program is running.
The other method doesn't work because your Form doesn't have the focus. If you want to use that method, I suggest that you have something on your Form that will have the Focus all the time. This might be a little tricky with all of the controls that you have on there.
My best advice is to use the first method. It's less of a hassle. Also, if you would like to use multiple-keypresses (like Alt + Ctrl + CapsLock) you can do that with this API.
Zero
May 22nd, 2000, 05:19 AM
You should use actual graphics commands for this, but I'll still work with your grid of buttons. On a similar note, you should change the buttons to SHAPE objects. These can't get the focus and bugger things up, and you can make them all kinds of kewl shapes like rounded boxes. An alternative would also be a bunch of image controls... But that's off the subject.
The keycodes in VB are as follows:
Up, 38
Down, 40
Left, 37
Right, 39
You would want something like this:
Form_Keydown(Shift as integer, Keycode as integer, blah blah)
If Keycode = 40 then
'That which will move thy block down
End if
If Keycode = 37 then
'That which will move thy block left
End if
If Keycode = 39 then
'That which will move thy block right
End if
End Sub
Also, the Keydown event "Skips." Hold down an arrow key and watch the cursor and you'll get what I mean. If you press and hold the button, the block will move one square, delay a second, and then rapidly continue to move. To remedy this, make a global variable (DIM it in the General Declarations section) called MoveDir as String. Then, you would have this:
Form_Keydown(Shift as integer, Keycode as integer, blah blah)
If Keycode = 40 then
MoveDir = "D"
End if
If Keycode = 37 then
MoveDir = "L"
End if
If Keycode = 39 then
MoveDir = "R"
End if
End Sub
Then in your timer:
Sub Timer1_Timer()
If MoveDir = "D" then
'Move block down 1 space
End if
If MoveDir = "L" then
'Move block left 1 space
End if
If MoveDir = "R" then
'Move block right 1 space
End if
End Sub
If you have any questions/problems/gripes/bitches/death threats, bother me at Z__E__R__O@Hotmail.com
ToneDogg
May 23rd, 2000, 12:06 AM
Thanks for replyin, I got it workin :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.