| View previous topic :: View next topic |
| Author |
Message |
Rizzit
Joined: 02 Nov 2009 Posts: 19
|
Posted: Tue Nov 03, 2009 3:23 pm Post subject: Help with Break command |
|
|
Hello I want to interuppt the loop of my script based on a key command.
So lets say if I press "P" it pauses the loop, if I press "P" again it continues
looping.
The whole Program looks like this:
| Code: | #NoEnv
F4::
MsgBox, 1,, Start VideoControl (Cancel to stop)
IfMsgBox OK
MsgBox Launching VideoControl Monitoring
run iexplore
Sleep, 10000
Send, username{TAB}
Send, password{ENTER} ; Auto Login Sequence
Sleep, 6000
Click 63, 240
Sleep, 60000 ; Thumbview actualizing time
loop
{
Sleep, 60000
Click 855, 955
Sleep, 60000
Click 825, 955
} |
The last part, the loop is looping 24/7 except if I need to make a config change on that PC or Site, so I really would like a pause/play function.
could you guys give me some hints in how this would go best?
Thank you all for your time for reading and helping me  |
|
| Back to top |
|
 |
Rizzit
Joined: 02 Nov 2009 Posts: 19
|
Posted: Tue Nov 03, 2009 3:23 pm Post subject: |
|
|
P.S: I thought at something like this
| Code: | loop
{
if {P}
break
Click
Sleep, 5000
Click
Sleep, 5000
} |
|
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 3113 Location: MN, USA
|
Posted: Tue Nov 03, 2009 3:56 pm Post subject: |
|
|
| How can a repeating action be stopped without exiting the script? AutoHotkey FAQ |
|
| Back to top |
|
 |
TLM
Joined: 21 Aug 2006 Posts: 2926 Location: The Shell
|
Posted: Tue Nov 03, 2009 4:15 pm Post subject: |
|
|
Look at this:
| Code: | Loop,
{
ToolTip, % "Key State is " keyState := GetKeyState("P", "")
If keyState = 1
{
ToolTip
Break
}
} |
This will stop the loop if P is pressed.
Here it is adapted:
| Code: |
Loop,
{
Click
Sleep, 5000
Click
Sleep, 5000
If keyState := GetKeyState("P", "")
Break
}
Return |
hth _________________
paradigm.shift:=(•_•)┌П┐RTFM||^.*∞ |
|
| Back to top |
|
 |
MasterFocus
Joined: 08 Apr 2009 Posts: 3035 Location: Rio de Janeiro - RJ - Brasil
|
Posted: Tue Nov 03, 2009 9:18 pm Post subject: |
|
|
TLM, your code doesn't really work.
Here's a working (tested) approach:
| Code: | Loop
{
Click
If KeyWasPressed("P",5000)
Break ; or Return
}
Return
KeyWasPressed(key,time)
{
start := A_TickCount
Loop
{
If GetKeyState(key)
return 1 ; true (key was pressed)
If ( A_TickCount - start >= time )
return 0 ; false (timed out)
}
} |
_________________ "Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried."
Antonio França
My stuff: Google Profile |
|
| Back to top |
|
 |
TLM
Joined: 21 Aug 2006 Posts: 2926 Location: The Shell
|
Posted: Tue Nov 03, 2009 11:52 pm Post subject: |
|
|
Yea I kinda figured the sleep would get in the way after the fact.
I should have added a key state condition as a workaround.
On a side not, I try to never use sleep . _________________
paradigm.shift:=(•_•)┌П┐RTFM||^.*∞ |
|
| Back to top |
|
 |
Rizzit
Joined: 02 Nov 2009 Posts: 19
|
Posted: Wed Nov 04, 2009 8:28 am Post subject: |
|
|
First of all thank you guys for your time
@MasterFocus, I tried to implement your code but it seems like I horribly
fail at this. My pc shutted down 3 times now. Could you please give me a
hint in where to put my loop in your code?
| Code: | loop
{
Sleep, 60000
Click 855, 955
Sleep, 60000
Click 825, 955
} |
Greetings, |
|
| Back to top |
|
 |
MasterFocus
Joined: 08 Apr 2009 Posts: 3035 Location: Rio de Janeiro - RJ - Brasil
|
Posted: Wed Nov 04, 2009 12:33 pm Post subject: |
|
|
You would have to use
Click 855, 955 instead of Click
and 60000 instead of 5000.
| Code: | Loop ; this is the loop you want
{
Click ; add/change coordinates as desired
; the following line calls the function I created, which
; will act like "Sleep, 5000" and break the loop if "P"
; is pressed - you may change the values as desired
If KeyWasPressed("P",5000)
Break
}
Return
;-----------------------------------------------
; add the following function to the bottom of your script
; so it can be called from inside that loop
KeyWasPressed(key,time)
{
start := A_TickCount
Loop
{
If GetKeyState(key)
return 1 ; true (key was pressed)
If ( A_TickCount - start >= time )
return 0 ; false (timed out)
}
} |
_________________ "Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried."
Antonio França
My stuff: Google Profile |
|
| Back to top |
|
 |
Rizzit
Joined: 02 Nov 2009 Posts: 19
|
Posted: Wed Nov 04, 2009 1:32 pm Post subject: |
|
|
Ok it seems much clearer now, at least the first part of your code
I tested it now and it interuppts the loop, unfortunately it closes the script
too at the moment which is surely because I didnt understand something
with the 2nd part.
So actually I have this input:
| Code: | Loop
{
Click 855, 955 ; Site 2
If KeyWasPressed("P",10000)
Break
Click 825, 955 ; Site 1
If KeyWasPressed("P",10000)
Break
}
Return
KeyWasPressed(key,time)
{
start := A_TickCount
Loop
{
If GetKeyState(key)
return 1 ; true (key was pressed)
If ( A_TickCount - start >= time )
return 0 ; false (timed out)
}
}
|
Do I have to input data in those (key) (key was pressed) etc. fields?
(I tried to input data in here but I think I dit not gave the correct paramaters) |
|
| Back to top |
|
 |
HotKeyIt
Joined: 18 Jun 2008 Posts: 4652 Location: AHK Forum
|
Posted: Wed Nov 04, 2009 3:31 pm Post subject: |
|
|
As I understood, you would like the loop to pause, not break, right?
Try this:
| Code: | Loop
{
Click 855, 955 ; Site 2
Sleep, 10000
SleepKey(pause)
Click 825, 955 ; Site 1
Sleep, 10000
SleepKey(pause)
}
Return
~p::pause := !pause
SleepKey(ByRef pause){
While % (pause){
Sleep, 100
}
} |
_________________ AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun  |
|
| Back to top |
|
 |
Rizzit
Joined: 02 Nov 2009 Posts: 19
|
Posted: Thu Nov 05, 2009 7:25 am Post subject: |
|
|
Thank you man works perfectly fine
To improve myself I would really like to understand this part of the code
| Code: | SleepKey(ByRef pause){
While % (pause){
Sleep, 100 |
greetings |
|
| Back to top |
|
 |
HotKeyIt
Joined: 18 Jun 2008 Posts: 4652 Location: AHK Forum
|
Posted: Thu Nov 05, 2009 9:18 am Post subject: |
|
|
| Rizzit wrote: | Thank you man works perfectly fine
To improve myself I would really like to understand this part of the code
| Code: | SleepKey(ByRef pause){
While % (pause){
Sleep, 100 |
greetings |
| A function receiving the variable ByRef (so the global variable -pause- is used) wrote: | | SleepKey(ByRef pause) |
| As long as pause = true (so not 0 or empty) -Wait for variable to change- wrote: | While % (pause){
Sleep, 100
... |
Hope it helps  _________________ AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun  |
|
| Back to top |
|
 |
Rizzit
Joined: 02 Nov 2009 Posts: 19
|
Posted: Thu Nov 05, 2009 10:51 am Post subject: |
|
|
ah cool Thank You man
[/list] |
|
| Back to top |
|
 |
Rizzit
Joined: 02 Nov 2009 Posts: 19
|
Posted: Thu Nov 05, 2009 11:09 am Post subject: |
|
|
| Code: | Loop
{
Click 855, 955 ; Site 2
Sleep, 10000
SleepKey(pause)
Click 825, 955 ; Site 1
Sleep, 10000
SleepKey(pause)
}
Return
~p::pause := !pause
SleepKey(ByRef pause){ |
Now I wanted to go further and implement a blinkling like tooltip that
starts when P gets pressed and stops when it gets pressed again.
The problem is inside this loop it confuses me...
I tried something like this:
| Code: | If GetKeyState("P" [, "P"])
Loop
{
ToolTip, Monitoring Currently Paused!
SetTimer, RemoveToolTip, 2000
}
Else
Return |
But I'm not sure if thats the right direction, could need some support again  |
|
| Back to top |
|
 |
HotKeyIt
Joined: 18 Jun 2008 Posts: 4652 Location: AHK Forum
|
Posted: Thu Nov 05, 2009 1:05 pm Post subject: |
|
|
 | Code: | Loop
{
Click 855, 955 ; Site 2
Sleep, 10000
SleepKey(pause)
Click 825, 955 ; Site 1
Sleep, 10000
SleepKey(pause)
}
Return
~p::
If !(pause := !pause){
SetTimer, ToolTip, Off
Goto, RemoveToolTip
} else
SetTimer,ToolTip,1000
Return
SleepKey(ByRef pause){
While % (pause){
Sleep, 100
}
}
ToolTip:
ToolTip,Paused !!!
SetTimer,RemoveToolTip,-500
Return
RemoveToolTip:
ToolTip
Return |
_________________ AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun  |
|
| Back to top |
|
 |
|