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 

Help with Break command
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Rizzit



Joined: 02 Nov 2009
Posts: 19

PostPosted: Tue Nov 03, 2009 3:23 pm    Post subject: Help with Break command Reply with quote

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 Wink
Back to top
View user's profile Send private message
Rizzit



Joined: 02 Nov 2009
Posts: 19

PostPosted: Tue Nov 03, 2009 3:23 pm    Post subject: Reply with quote

P.S: I thought at something like this


Code:
loop
{
    if {P}
        break
    Click
    Sleep, 5000
    Click
    Sleep, 5000
}
Back to top
View user's profile Send private message
jaco0646



Joined: 07 Oct 2006
Posts: 3113
Location: MN, USA

PostPosted: Tue Nov 03, 2009 3:56 pm    Post subject: Reply with quote

How can a repeating action be stopped without exiting the script? AutoHotkey FAQ
Back to top
View user's profile Send private message Visit poster's website
TLM



Joined: 21 Aug 2006
Posts: 2926
Location: The Shell

PostPosted: Tue Nov 03, 2009 4:15 pm    Post subject: Reply with quote

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
View user's profile Send private message
MasterFocus



Joined: 08 Apr 2009
Posts: 3035
Location: Rio de Janeiro - RJ - Brasil

PostPosted: Tue Nov 03, 2009 9:18 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
TLM



Joined: 21 Aug 2006
Posts: 2926
Location: The Shell

PostPosted: Tue Nov 03, 2009 11:52 pm    Post subject: Reply with quote

Embarassed 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 Wink.
_________________
paradigm.shift:=(•_•)┌П┐RTFM||^.*∞
Back to top
View user's profile Send private message
Rizzit



Joined: 02 Nov 2009
Posts: 19

PostPosted: Wed Nov 04, 2009 8:28 am    Post subject: Reply with quote

First of all thank you guys for your time Wink

@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
View user's profile Send private message
MasterFocus



Joined: 08 Apr 2009
Posts: 3035
Location: Rio de Janeiro - RJ - Brasil

PostPosted: Wed Nov 04, 2009 12:33 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Rizzit



Joined: 02 Nov 2009
Posts: 19

PostPosted: Wed Nov 04, 2009 1:32 pm    Post subject: Reply with quote

Ok it seems much clearer now, at least the first part of your code Very Happy
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
View user's profile Send private message
HotKeyIt



Joined: 18 Jun 2008
Posts: 4652
Location: AHK Forum

PostPosted: Wed Nov 04, 2009 3:31 pm    Post subject: Reply with quote

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 Wink
Back to top
View user's profile Send private message
Rizzit



Joined: 02 Nov 2009
Posts: 19

PostPosted: Thu Nov 05, 2009 7:25 am    Post subject: Reply with quote

Thank you man works perfectly fine Wink

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
View user's profile Send private message
HotKeyIt



Joined: 18 Jun 2008
Posts: 4652
Location: AHK Forum

PostPosted: Thu Nov 05, 2009 9:18 am    Post subject: Reply with quote

Rizzit wrote:
Thank you man works perfectly fine Wink

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 Wink
_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
Rizzit



Joined: 02 Nov 2009
Posts: 19

PostPosted: Thu Nov 05, 2009 10:51 am    Post subject: Reply with quote

ah cool Thank You man Wink

[/list]
Back to top
View user's profile Send private message
Rizzit



Joined: 02 Nov 2009
Posts: 19

PostPosted: Thu Nov 05, 2009 11:09 am    Post subject: Reply with quote

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 Confused
Back to top
View user's profile Send private message
HotKeyIt



Joined: 18 Jun 2008
Posts: 4652
Location: AHK Forum

PostPosted: Thu Nov 05, 2009 1:05 pm    Post subject: Reply with quote

Wink
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 Wink
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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