|
-
Aug 13th, 2000, 05:36 PM
#1
Thread Starter
Junior Member
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
-
Aug 13th, 2000, 05:39 PM
#2
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 13th, 2000, 05:57 PM
#3
Thread Starter
Junior Member
-
Aug 13th, 2000, 06:02 PM
#4
Monday Morning Lunatic
Add a Label and a CommandButton. Then add the following code to the form:
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
Run it, then press the button. Hit escape when you want it to stop.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 13th, 2000, 06:05 PM
#5
Declare a module level variable that you can use as a flag:
Code:
Private blnEscapePressed As Boolean
Set this flag to True in the KeyDown event if the key pressed is Escape:
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyEscape Then
blnEscapePressed = True
End If
End Sub
Check for this flag in your loop and if it's set to True then exit:
Code:
Do While True
'your code
DoEvents
If blnEscapePressed Then
Exit Do
End If
Loop
Use Exit For if you have a For..Next loop instead of a Do loop.
You must set the KeyPreview property of the Form to True for this to work!
Good luck!
-
Aug 13th, 2000, 06:07 PM
#6
Monday Morning Lunatic
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 13th, 2000, 06:54 PM
#7
Thread Starter
Junior Member
ok
but my loop is sill going
-
Aug 13th, 2000, 06:57 PM
#8
Monday Morning Lunatic
Are you sure that the flag variable is a GLOBAL variable?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 13th, 2000, 07:02 PM
#9
Thread Starter
Junior Member
-
Aug 13th, 2000, 09:21 PM
#10
Thread Starter
Junior Member
Ok
Thanks Joacim Andersson, That code looks like what I want, but it still doesn't work! Please someone HELP ME!!!!!!!
-
Aug 14th, 2000, 06:28 AM
#11
Can you post your loop code here? It's much easier to help if we get your code.
-
Aug 14th, 2000, 07:08 AM
#12
Thread Starter
Junior Member
No I dont really want my loop to be seen by everyone that looks but I will email you if you dont mind.
-
Aug 14th, 2000, 07:11 AM
#13
Addicted Member
Don't be shy Droidking!
My code is crap as well
-
Aug 14th, 2000, 07:30 AM
#14
Thread Starter
Junior Member
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)
-
Aug 14th, 2000, 07:53 AM
#15
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.
Code:
Public blnEscapePressed As Boolean
Now it should work!
Good luck!
-
Aug 14th, 2000, 07:59 AM
#16
Thread Starter
Junior Member
Sorry
Still doen't manybe its my computer can someone else tryit Please!
-
Aug 14th, 2000, 08:48 AM
#17
Thread Starter
Junior Member
AAAAAAAAAHHHHHHHHHH!!!!!!!!!!!!
AAAAAAAAAAAHHHHHHHHHHHHHHHHHHHHHHHHH
Im Going CRAZY trying to get this #&$@ing program to work can some one please help!
Always let the Wookiee Win!
-
Aug 14th, 2000, 09:10 AM
#18
_______
<?>
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.
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
blnEscapePressed = False
If vbKeyEscape Then
blnEscapePressed = True
MsgBox blnEscapePressed
End If
End Sub
if you don't click the command with the loop it messages
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.
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Aug 14th, 2000, 09:20 AM
#19
Thread Starter
Junior Member
-
Aug 14th, 2000, 09:23 AM
#20
Frenzied Member
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
Code:
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
and this in your loop
Code:
DoEvents
If EscapeIsPressed Then
Exit Do
End If
don't forget to unload or re-show your form again after the loop has exited.
-
Aug 14th, 2000, 09:26 AM
#21
_______
<?>
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.
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Aug 14th, 2000, 09:39 AM
#22
Thread Starter
Junior Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|