View Full Version : Key Up Event
NeroMaj
10-12-2010, 01:04 PM
I am working a little VBA game in Word and I am running into some problems. I have the following procedure
Private Sub UserForm_KeyUp(ByVal KeyCode As MsForms.ReturnInteger, ByVal Shift As Integer)
GameOver = False
If KeyCode = 112 Then
numSections = 0
lblTitle.Visible = False
lblClick.Visible = False
ShowPiece
BuildSnake
ElseIf KeyCode = 37 Then
While KeyCode = 37 And GameOver = False
For i = 1 To numSections
If i = 1 Then
frmSnake.Controls("img" & i).Left = frmSnake.Controls("img" & i).Left - 1
Wait
Else
frmSnake.Controls("img" & i).Left = x
frmSnake.Controls("img" & i).Top = y
Wait
x = img.Left
y = img.Top
End If
Next
CheckCollision
Wend
ElseIf KeyCode = 38 Then
While KeyCode = 38 And GameOver = False
For i = 1 To numSections
If i = 1 Then
frmSnake.Controls("img" & i).Top = frmSnake.Controls("img" & i).Top - 1
Wait
Else
frmSnake.Controls("img" & i).Left = x
frmSnake.Controls("img" & i).Top = y
Wait
x = img.Left
y = img.Top
End If
Next
CheckCollision
Wend
ElseIf KeyCode = 39 Then
While KeyCode = 39 And GameOver = False
For i = 1 To numSections
If i = 1 Then
frmSnake.Controls("img" & i).Left = frmSnake.Controls("img" & i).Left + 1
Wait
Else
frmSnake.Controls("img" & i).Left = x
frmSnake.Controls("img" & i).Top = y
Wait
x = img.Left
y = img.Top
End If
Next
CheckCollision
Wend
ElseIf KeyCode = 40 Then
While KeyCode = 40 And GameOver = False
For i = 1 To numSections
If i = 1 Then
frmSnake.Controls("img" & i).Top = frmSnake.Controls("img" & i).Top + 1
Wait
Else
frmSnake.Controls("img" & i).Left = x
frmSnake.Controls("img" & i).Top = y
Wait
x = img.Left
y = img.Top
End If
Next
CheckCollision
Wend
End If
End Sub
I need to know how to exit the embedded while loops if a different key is pressed.
fumei
10-12-2010, 01:28 PM
"I need to know how to exit the embedded while loops if a different key is pressed."
Huh?
You should be going into those loops unless a specific key is pressed. For example:
ElseIf KeyCode = 40 Then
While KeyCode = 40 And GameOver = False
If KeyCode is not 40 then the While is not executed. I do not understand the question.
You may want to consider using Select Case instead of your If..Else If statements.
Select Case KeyCode
Case 112
numSections = 0
lblTitle.Visible = False
lblClick.Visible = False
ShowPiece
BuildSnake
Case 37
While KeyCode = 37 And GameOver = False
' etc. etc.
fumei
10-12-2010, 01:30 PM
BTW: you are using a KeyUp on the userform? Normally, but not always, KeyPress events are used in specific controls, rather than the userform itself. Care to elaborate?
NeroMaj
10-19-2010, 11:49 AM
I assumed it would be clear from the block of code that I posted and my description what my intentions were.
To elaborate, the game I am designing is similar to the old snake game where you start with one piece and you collect more pieces and the snake gets longer and longer until you either run into yourself or run into the wall. The idea is to have the user press a direction and have the "snake" move in that direction until a different direction is pressed. This way, the user can't just move one space and stop.
Is there any way to check if a key is pressed while inside of a loop like the ones in the code I posted?
fumei
10-19-2010, 12:22 PM
"I assumed it would be clear from the block of code that I posted and my description what my intentions were."
Nope.
And sorry, you have not elaborated whatsoever. Do you really think:
"The idea is to have the user press a direction and have the "snake" move in that direction until a different direction is pressed"
elaborates or explains anything? Press a direction? Do you mean press a control on the userform? Do you mean press a key? If you mean key...say key. "Direction" is meaningless unless defined.
You fail to say what happens with a key down, but one could assume (always a BAD thing) that the DOWN key is a "good" action? Maybe? What about Down? You have to have a Down before you have an UP.
"Is there any way to check if a key is pressed while inside of a loop like the ones in the code I posted?"
No, how could there be? NO keypress - of any sort - is processed until the current procedure (in this case your Key Up) terminates.
In other words:
keypress (up OR down)
keypress event fires - NO keypress event happens until this finishes!
Or, in other other words, while inside the loop, there ARE no other key presses.
I will state it again. It would be better to use a Select Case rather than your If Else statement.
What is numSections?
x = img.Left
y = img.Top
I presume x and y are declared globally elsewhere? Yes?
What is img? Obviously a control of some kind. But what? What makes it different (and does not seem to require a declaration) from
frmSnake.Controls("img" & i).Left
which IS fully qualified. What is the difference - if any - between
frmSnake.Controls("img" & i).Left
and
img.Left
Why does img not require frmSnake.Controls???
Thanks for elaborating.
fumei
10-19-2010, 12:28 PM
Although really, the answer to your posted question:
"I need to know how to exit the embedded while loops if a different key is pressed."
is simple. You can not, because it is not possible for a different key to BE pressed while the procedure is running. The procedure must terminate before ANY other event happens.
Press (whatever) the keys like so:
k - down
k - up
d - down
d - up
The key press events action ONE key at a time.
k - up MUST finish before d-down starts.
NeroMaj
10-19-2010, 12:41 PM
Wow...way to make someone feel welcome to ask questions and get information from this site :(
Obviously direction would mean the arrows on the keyboard since my entire thread is about key events and not once have I said anything about buttons or any other type of control on the form.
As far as the Select Case over If Else Statement, in this case it will not make any difference because a) both have the desired of effect of moving the "snake" in the direction of the key pressed and b) neither can achieve the desired goal.
As far as the discrepancy between img and "img" & i, this has since been fixed and the img is no longer there.
Now, to elaborate even further (I will make this very simple to understand)...
I have an image control on a form. Once the userform is initialized the image is present on the form. What I want to achieve is to have the user be able to press (either KeyDown, KeyUp, or KeyPress) an arrow KEY and have the image "move" in that direction (by "move" i mean shift either the .left or .top property and redraw the image to give the illusion of moving based on what arrow KEY is pressed).
If the image "moves" too far (meaning it hits the borders of the frame, the game ends). If the user presses a subsequent KEY (another arrow KEY), I want the direction that the image is "moving" in to change to the direction of the newlty pressed KEY.
My problem, as I said before is some way to change the direction of the image while it is moving. I am open to any ways to accomplish this goal, not necessarily KeyPress Events.
Note: Don't yell at me because you don't understand what I am talking about. Obviously I don't have the technical knowledge of VBA that you do, but you are asking details that are outside of the core of my question and aren't even required to answer it. I try to give as much information as necessary to get good replies to my questions and if you need more information you can just ask and I'll be glad to provide it, but snapping at people is just rude.
fumei
10-19-2010, 12:43 PM
Although really, the answer to your posted question:
"I need to know how to exit the embedded while loops if a different key is pressed."
is simple. You can not, because it is not possible for a different key to BE pressed while the procedure is running. The procedure must terminate before ANY other event happens.
Press (whatever) the keys like so:
k - down
k - up
d - down
d - up
The key press events action ONE key at a time.
k - up MUST finish before d-down starts.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.