Hey
I have a loop with some stuff in it and know I was hoping some one chould tell me how to make it so when I press Esc the loop stops
Printable View
Hey
I have a loop with some stuff in it and know I was hoping some one chould tell me how to make it so when I press Esc the loop stops
If you stick in a DoEvents somewhere in your loop, then put a definition for the Form_KeyPress event, and check for the Escape key.
Ok
how do I do that?
Add a Label and a CommandButton. Then add the following code to the form:
Run it, then press the button. Hit escape when you want it to stop.Code:Dim gbStop As Boolean
Private Sub Command1_Click()
Dim i As Long
For i = 1 To 100000
DoEvents
If gbStop Then Exit For
Label1.Caption = i
Next i
End Sub
Private Sub Command1_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyEscape Then gbStop = True
End Sub
Private Sub Form_Load()
gbStop = False
End Sub
Declare a module level variable that you can use as a flag:
Set this flag to True in the KeyDown event if the key pressed is Escape:Code:Private blnEscapePressed As Boolean
Check for this flag in your loop and if it's set to True then exit:Code:Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyEscape Then
blnEscapePressed = True
End If
End Sub
Use Exit For if you have a For..Next loop instead of a Do loop.Code:Do While True
'your code
DoEvents
If blnEscapePressed Then
Exit Do
End If
Loop
You must set the KeyPreview property of the Form to True for this to work!
Good luck!
Take a look at this thread: http://forums.vb-world.net/showthrea...threadid=26347
ok
but my loop is sill going
Are you sure that the flag variable is a GLOBAL variable?
A GLOBAL variable?
Ok
Thanks Joacim Andersson, That code looks like what I want, but it still doesn't work! Please someone HELP ME!!!!!!!
Can you post your loop code here? It's much easier to help if we get your code.
No I dont really want my loop to be seen by everyone that looks but I will email you if you dont mind.
Don't be shy Droidking!
My code is crap as well :)
Ok Ok here is my code if you can help please do! dont laughor ill have to change me user name!
Dim t As Long
-----------------------------------------------------------
Sub MouseDown(Optional Left As Boolean = True, Optional Right As Boolean, Optional Middle As Boolean, Optional Click As Boolean)
mouse_event -Left * 2 - Right * 8 - Middle * 32, 0&, 0&, 0&, 0&
If Click Then mouse_event -Left * 4 - Right * 16 - Middle * 64, 0, 0, 0, 0
End Sub
-----------------------------------------------------------
Sub MouseUp(Optional Left As Boolean = True, Optional Right As Boolean, Optional Middle As Boolean)
mouse_event -Left * 4 - Right * 16 - Middle * 64, 0, 0, 0, 0
End Sub
-----------------------------------------------------------
Private Sub Command1_Click()
Form1.Hide
x = 20
y = 40
Do While True
If x = 450 Then x = 0
If y = 450 Then y = 0
x = x + 5
y = y + 5
t = SetCursorPos(x, y)
If x = 300 Then MouseDown
If x = 300 Then MouseUp
If x = 300 Then MouseDown
If x = 300 Then MouseUp
Call Sleep(75)
DoEvents
If blnEscapePressed Then
Exit Do
End If
Loop
End Sub
-----------------------------------------------------------
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyEscape Then
blnEscapePressed = True
End If
End Sub
------------------------------------------------------------ This is a Module
Private blnEscapePressed As Boolean
Declare Function SetCursorPos& Lib "user32" (ByVal x As Long, ByVal y As Long)
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
You put the declaration of the blnEscapePressed in a module outside of the form. So it can't be used in the form. The solution is to change the declaration from Private into Public.
Now it should work!Code:Public blnEscapePressed As Boolean
Good luck!
Sorry
Still doen't manybe its my computer can someone else tryit Please!
AAAAAAAAAAAHHHHHHHHHHHHHHHHHHHHHHHHH
Im Going CRAZY trying to get this #&$@ing program to work can some one please help!
I don't have the answer but I can tell you why you are pulling your hair out..
Once inside the loop the app doesn't recoginize anything outside the loop...how to get around it is a horse of a different color.
ie.
if you don't click the command with the loop it messagesCode:Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
blnEscapePressed = False
If vbKeyEscape Then
blnEscapePressed = True
MsgBox blnEscapePressed
End If
End Sub
and it changes the value of blnescapepressed.
however once you click the command button
it doesnt' work...why??? that is the question
not the code or variables...
how do you get inside the loop is the question.
:):):):):):):)Thanks:):):):):):):)
Ok so if there is no way to get out of a loop is there something I can use instead of a loop????
Apart from scope problems,
You are trying to detect a kepress in the form but the form is hidden so the event is never triggered.
try this instead:
paste this in your form
and this in your loopCode:Private Declare Function GetKeyState Lib "user32.dll" (ByVal nVirtKey As Long) As Integer
Function EscapeIsPressed() As Boolean
Dim keystate As Integer
keystate = GetKeyState(vbKeyEscape)
' Check the &H8000 bit of the return value.
EscapeIsPressed = keystate And &H8000
End Function
don't forget to unload or re-show your form again after the loop has exited.Code:DoEvents
If EscapeIsPressed Then
Exit Do
End If
the problem with your code is in the
Me.Hide
since you are asking it to change the value from a form event Ie..form keypress...and the form is invisible it can't work...it you leave the form visible then your code will work.
:):):):):)THANKS!!:):):):):)
Thanks man it worked! your lucky you aren't here Because I chould kiss you(At least a Huge)!!!!!YOU RULE!!!!!!
And HeSaidJoe Thanks for your help too