AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Breaking an infinite loop with keypress

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
anon
Guest





PostPosted: Mon May 14, 2007 4:30 pm    Post subject: Breaking an infinite loop with keypress Reply with quote

Hello.

I would like to execute a loop infinitely, but I would like this loop to stop as soon as a key (any random key) is pressed. I found a similar code on the documentation, but that code executes a loop when a key is held down. Here is what I have so far:

Code:

WinWait, NetWinner.com - Windows Internet Explorer,
IfWinNotActive, NetWinner.com - Windows Internet Explorer, , WinActivate, NetWinner.com - Windows Internet Explorer,
WinWaitActive, NetWinner.com - Windows Internet Explorer,
$F1::  ; Make the F1 key into a hotkey (the $ symbol facilitates the "P" mode of GetKeyState below).
Loop  ; Since no number is specified with it, this is an infinite loop unless "break" or "return" is encountered inside.
{
    if not GetKeyState("F1", "P")  ; If this statement is true, the user has physically released the F1 key.
        break  ; Break out of the loop.
    ; Otherwise (since the above didn't "break"), keep clicking the mouse.
MouseClick, left,  451,  527
Sleep, 17000
MouseClick, left,  607,  525
Sleep, 14000

}
return

[Moderator's note - Added code tags]

I have already tried taking out the "not" because I thought that would make the code in parenthesis a true statement. That didnt work. Anyone have any suggestions? Ive already tried searching.

Thanks.
Back to top
engunneer



Joined: 30 Aug 2005
Posts: 6804
Location: Pacific Northwest, US

PostPosted: Mon May 14, 2007 4:50 pm    Post subject: Reply with quote

This is an extremelly common question. What search terms did you use?

you need on ly a slight variation on the code from this recent thread: http://www.autohotkey.com/forum/viewtopic.php?t=19130&highlight=loop


Code:

Loop,
{
if (BreakLoop = 1)
  break
;rest of loop code

}


elsewhere
Code:

Esc::
BreakLoop = 1
return

This will break on Escape key.


Also search for the word Suspend, it may help you.
_________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM
Back to top
View user's profile Send private message Visit poster's website
ScottEdge



Joined: 14 Aug 2005
Posts: 57
Location: Connecticut,USA

PostPosted: Wed May 16, 2007 3:54 pm    Post subject: Reply with quote

For future reference I found I had to put a sleep in my loop for this to work.

Code:

LOOP
{
if (BreakLoop = 1)
  break
SEND,{END}
SEND,{SPACE}
SEND,{DOWN}
}


Esc would not break this loop.

Code:

LOOP
{
if (BreakLoop = 1)
  break
SEND,{END}
SEND,{SPACE}
SEND,{DOWN}
sleep,100
}


Escape would break this loop
Back to top
View user's profile Send private message
tic



Joined: 22 Apr 2007
Posts: 1365

PostPosted: Tue Sep 18, 2007 11:05 am    Post subject: Reply with quote

I dont think thats what he actually asked (even though he appears to have gone now).

Quote:
I would like to execute a loop infinitely, but I would like this loop to stop as soon as a key (any random key) is pressed.


so he wants a loop to break on any key being pressed, not esc.

I think he meant something like this:

Code:
#Persistent
SetBatchLines, -1
SetKeyDelay, -1
OnExit, HandleExit

SetFormat, Integer, Hex

ThreadID := DllCall("GetCurrentThreadId")
VarSetCapacity(State, 256)
SetTimer, MonitorKeyboard, 30

Loop
{
   Sleep, 100
   If BreakLoop
   Break
   Tooltip, We are in the loop
}
Tooltip, The loop has been broken by a keypress
Return

;################################################################################

MonitorKeyboard:
TID := DllCall("GetWindowThreadProcessId", "UInt", WinActive("A"), "UInt", 0)

If (TID != TIDOld)
{
   DllCall("AttachThreadInput", "UInt", ThreadID, "UInt", TIDOld, "Int", 0)
   DllCall("AttachThreadInput", "UInt", ThreadID, "UInt", TID, "Int", 1)
   TIDOld := TID
}
DllCall("GetKeyboardState", "UInt", &State)

State := ""
Loop, 254
{
   If (*(&State+A_Index) & 0x80)
   State := A_Index
}

If State
{
   If State not in 0x1,0x2,0x4
   BreakLoop = 1
}
Return

;################################################################################

HandleExit:
DllCall("AttachThreadInput", "UInt", ThreadID, "UInt", TIDOld, "Int", 0)
ExitApp


so run that and the tooltip will inform you that youre in a loop, and when you press any key itll break. have only specified that left/right/middleclick on mouse dont break the loop. any other mouse buttons would need to be added

Edit: Maybe he didnt mean that Wink i hate it when people ask something and then just leave, and dont even say thank you or "that worked" or "that didnt work" ! Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group