AutoHotkey Community

It is currently May 26th, 2012, 11:05 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Help with Break command
PostPosted: November 3rd, 2009, 4:23 pm 
Offline

Joined: November 2nd, 2009, 1:47 pm
Posts: 19
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:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 3rd, 2009, 4:23 pm 
Offline

Joined: November 2nd, 2009, 1:47 pm
Posts: 19
P.S: I thought at something like this


Code:
loop
{
    if {P}
        break
    Click
    Sleep, 5000
    Click
    Sleep, 5000
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 3rd, 2009, 4:56 pm 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
How can a repeating action be stopped without exiting the script? AutoHotkey FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 3rd, 2009, 5:15 pm 
Offline

Joined: August 21st, 2006, 7:07 pm
Posts: 2925
Location: The Shell
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

_________________
Imageparadigm.shift:=(•_•)┌П┐RTFM||^.*∞


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 3rd, 2009, 10:18 pm 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
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.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 4th, 2009, 12:52 am 
Offline

Joined: August 21st, 2006, 7:07 pm
Posts: 2925
Location: The Shell
:oops: 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 ;).

_________________
Imageparadigm.shift:=(•_•)┌П┐RTFM||^.*∞


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 4th, 2009, 9:28 am 
Offline

Joined: November 2nd, 2009, 1:47 pm
Posts: 19
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,


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 4th, 2009, 1:33 pm 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
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.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 4th, 2009, 2:32 pm 
Offline

Joined: November 2nd, 2009, 1:47 pm
Posts: 19
Ok it seems much clearer now, at least the first part of your code :D
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)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 4th, 2009, 4:31 pm 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
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:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 5th, 2009, 8:25 am 
Offline

Joined: November 2nd, 2009, 1:47 pm
Posts: 19
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 5th, 2009, 10:18 am 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
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 ;)

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 5th, 2009, 11:51 am 
Offline

Joined: November 2nd, 2009, 1:47 pm
Posts: 19
ah cool Thank You man ;)

[/list]


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 5th, 2009, 12:09 pm 
Offline

Joined: November 2nd, 2009, 1:47 pm
Posts: 19
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 :?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 5th, 2009, 2:05 pm 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
;)
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:


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google [Bot], JSLover, Kirtman, Leef_me, Miguel, XstatyK and 57 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group