Sign in to follow this  
Followers 0
J0ker

GLobal If Command

29 posts in this topic

#1 ·  Posted (edited)

I was wondering if there is a GLobal If function. A If that could happen anywhere in the script. ie:

If the mouse dont do anything (dont move, dont click) during x time then Do that.

I'm looking for this kind of command because sometime my script encounter an error ( missed a command or a function) and the mouse stop. The script dont close because there's no real "error" but the mouse is stuck. Does a Global If function exist that cover all the script?

ie:

If ;Something happen anywhere at anytime in the script  ( in this case, mouse dont move during x time)
Then
; Do that
Edited by J0ker

Share this post


Link to post
Share on other sites



You could try something like this:

while 1 
    $pos = MouseGetPos()
    sleep(3000)
    $var_2 = MouseGetPos()
    if $pos[0] = $var_2[0] AND $pos[1] = $var_2[1] Then MsgBox(1,"STOP","Mouse isn't moving...")    
WEnd

Neo


[center][font="Arial"]--- The Neo and Only --- [/font][font="Arial"]--Projects---[/font]Image to Text converterText to ASCII converter[/center]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thx Neo ! Wich parameter should I add if I want to add time ( If Mouse dont move during x time)?

Edited by J0ker

Share this post


Link to post
Share on other sites

You need to use a global variable that you keep writing a time value (_TicksToTime) to constantly when the mouse is not moving (AdlibEnable). Then you will need to have a test in your functions or loops that when the variable is greater than X do Y (reset global as well back to 0).

Anyway that is one approach.

Kohr

Share this post


Link to post
Share on other sites

You need to use a global variable that you keep writing a time value (_TicksToTime) to constantly when the mouse is not moving (AdlibEnable). Then you will need to have a test in your functions or loops that when the variable is greater than X do Y (reset global as well back to 0).

Anyway that is one approach.

Kohr

Yes I think that could work but where do I write my If? I dont want to copy the same test every 5 lines or so. Is there's a command that will cover all my script?

Share this post


Link to post
Share on other sites

I was wondering if there is a GLobal If function. A If that could happen anywhere in the script. ie:

If the mouse dont do anything (dont move, dont click) during x time then Do that.

I'm looking for this kind of command because sometime my script encounter an error ( missed a command or a function) and the mouse stop. The script dont close because there's no real "error" but the mouse is stuck. Does a Global If function exist that cover all the script?

ie:

If ;Something happen anywhere at anytime in the script  ( in this case, mouse dont move during x time)
Then
; Do that
I haven't used this in AutoIt but there are OnEvent commands/functions you could try. I have used them in VisualBasic for Access.

Check under the help file for this Opt("GUIOnEventMode", 1). I just did a search on the word event.

You need to be careful when you code or use functions that happen on an event. Don't make the code long if it is an event that will happen alot. Make it short and to the point because it will happen (or is suppsed to) every time that event occurs.

Hope that helps. I don't quite understand what is happening with your script enough to help you out more but see if this helps and leave more info if you need further help.


Be open minded but not gullible.A hammer sees everything as a nail ... so don't be A tool ... be many tools.

Share this post


Link to post
Share on other sites

Yes I think that could work but where do I write my If? I dont want to copy the same test every 5 lines or so. Is there's a command that will cover all my script?

Look up AdlibEnable in the AutoIT documentation. You can use:

AdlibEnable("function",300)
; beginning of period you want to check
...
; end of period you want to check
AdlibDisable()
...
Func function()
  ;if mouse is stopped then do X
EndFunc

It calls the function every so often for the time period set in AdlibEnable("function",time) where time is in ms. Put the conditional in the function and it will be called "globally" every time ms.

Share this post


Link to post
Share on other sites

Yes I think that could work but where do I write my If? I dont want to copy the same test every 5 lines or so. Is there's a command that will cover all my script?

Your "If" will be in every function and loop. Possible just put in AdlibEnable but it may be very slow. Check help file for AdlibEnable.

Kohr

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Should I put this into my loop or not?

AdlibEnable("ScriptStuck", 500)


Func ScriptStuck()
$pos = MouseGetPos()
$var_2 = MouseGetPos()
if $pos[0] = $var_2[0] AND $pos[1] = $var_2[1] then Main()
EndFunc

I just need to add a time variable to the function so it check if the mouse is not moving during 1 minutes.

If I add this AdlibEnable into my script, does it will stop the script and wait 1 minutes to see if the mouse is not moving or it will continu the rest of my script while doing this check?

Edited by J0ker

Share this post


Link to post
Share on other sites

Should I put this into my loop or not?

AdlibEnable("ScriptStuck", 500)
Func ScriptStuck()
$pos = MouseGetPos()
$var_2 = MouseGetPos()
if $pos[0] = $var_2[0] AND $pos[1] = $var_2[1] then Main()
EndFunc

I just need to add a time variable to the function so it check if the mouse is not moving during 1 minutes.

If I add this AdlibEnable into my script, does it will stop the script and wait 1 minutes to see if the mouse is not moving or it will continu the rest of my script while doing this check?

I imagine you want $pos to be outside the function in the rest of the script (so that they are executed with a time interval between them) as you are checking for a difference in values. If $pos is outside the function and $var_2 is in then $pos will be checked in your loop, every 500 ms $var_2 will be evaluated and checked. The rest of your script will continue to execute. Adlib just runs periodically between Enable and Disable, hence your conditional will be checked every 500 milliseconds.

Share this post


Link to post
Share on other sites

Look at this example for ideas.

Global $MouseBase[2]
Global $TimeBase
Global $TimeTest = 0


AdlibEnable("ScriptStuck", 500)
GUISetState()
While 1
    Sleep(200)
WEnd

Func ScriptStuck()
    $pos = MouseGetPos()
    If $pos[0] = $MouseBase[0] Then
        If $pos[1] = $MouseBase[1] Then
            If $TimeTest = 0 Then
            ;mouse not moving since last time base was set
                $TimeBase = TimerInit()
                $TimeTest = 1
            Else
                $TimeDif = TimerDiff($TimeBase)
                If $TimeDif > 5000 Then ;5 seconds
                    MsgBox(0,"","Mouse not moving")
                EndIf
            EndIf
        Else
            $MouseBase[0] = $pos[0]
            $MouseBase[1] = $pos[1]
            $TimeTest = 0
        EndIf
    Else
        $MouseBase[0] = $pos[0]
        $MouseBase[1] = $pos[1]
        $TimeTest = 0
    EndIf
EndFunc

Kohr

Share this post


Link to post
Share on other sites

Yes like Kohr says or a simplistic way, I'm not sure:

; Beginning script stuff - This will happen only one time

; obtain the original position of mouse
$pos = MouseGetPos()

AdlibEnable("ScriptStuck", 500)

; Rest of script that will run while you keep checking mouse movement every 500 ms....

AdlibDisable()

; Rest of script where you don't care about mouse movement or end of script...

Func ScriptStuck()
; check new position of mouse
$var_2 = MouseGetPos()
; check if new position is same as old, if so jump to Main()
if $pos[0] = $var_2[0] AND $pos[1] = $var_2[1] then Main()
; if different save new position for next check
else $pos = $var_2
EndFunc

Func Main()  ; Main function
While
; doing other stuff and not checking mouse position....
Wend
Endfunc

After the first check, all subsequent checks should be the time frame you decide apart.

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

If Instead of checking everytime if the mouse is stuck during 2 minutes we could return values.

1 ms = 0,001 s

x ms = 120 s ( 2min)

x= 120000 ms

20 checks = 120000 / 20

1 check = Every 6 000ms

If AdlibEnable return 20 true (that would mean that the mouse isnt moving till 2 minutes). The code will me much more easy to write and there will be no slow time for the script.

$TimeStack = ;variable to stack true values returned by the function

AdlibEnable("ScriptStuck", 6000)


Func ScriptStuck()
$pos = MouseGetPos()
$var_2 = MouseGetPos()
if $pos[0] = $var_2[0] AND $pos[1] = $var_2[1] then 

;If Func ScriptStuck is true, return 1 to $Timestack, if not  restart $Timestack to 0
If $Timestack = 20 then Main()


EndFunc

If Func ScriptStuck is true, return 1 to $Timestack, if not restart $Timestack to 0

If $Timestack = 20 then Main()

I dont know how to code it...but if anyone can help me :P

Edited by J0ker

Share this post


Link to post
Share on other sites

Can anyone help me making this little code?

Thx!

Share this post


Link to post
Share on other sites

Can anyone help me making this little code?

Thx!

$TimeStack = 0

AdlibEnable("ScriptStuck", 6000)

Func ScriptStuck()
    $pos = MouseGetPos()
    $var_2 = MouseGetPos()
    If $pos[0] = $var_2[0] And $pos[1] = $var_2[1] Then $TimeStack += 1
    If $Timestack = 20 Then Main()
EndFunc

IE Dev ToolbarMSDN: InternetExplorer ObjectMSDN: HTML/DHTML Reference Guide[quote]It is surprising what a man can do when he has to, and how little most men will do when they don't have to. - Walter Linn[/quote]--------------------[font="Franklin Gothic Medium"]Post a reproducer with less than 100 lines of code.[/font]

Share this post


Link to post
Share on other sites

$TimeStack = 0

AdlibEnable("ScriptStuck", 6000)

Func ScriptStuck()
    $pos = MouseGetPos()
    $var_2 = MouseGetPos()
    If $pos[0] = $var_2[0] And $pos[1] = $var_2[1] Then $TimeStack += 1
    If $Timestack = 20 Then Main()
EndFunc
Thx Mike! I want to add an "else" so if both of these "If" doesnt work then $Timestack restart to 0. If I try to add "else" in the function then i'm getting new errors because it cut the function in two.

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

I'm almost finish making this code but I still have a problem. Habitually I can add If and else in a function without breaking the structure of it but in this time, the Func and EndFunc are not together. I think it's because there's no loop involved but I can't correct it. How to correct that without ruining the code and getting the same result?

AdlibEnable("ScriptStuck", 6000)

Posted Image

Edited by J0ker

Share this post


Link to post
Share on other sites

Here's a really cool little script for measuring idle states. I forget who I pilfered it from, but I THINK it was originally by Larry.

HotKeySet("{Esc}", "_Terminate")

Dim $last_active = 0
Dim $timer = TimerInit()

While (1)
    $not_idle = _CheckIdle($last_active)
    If $not_idle <> 0 Then $timer = TimerInit()
    ToolTip(Int(TimerDiff($timer) / 1000))
    Sleep(200)
WEnd

Func _CheckIdle(ByRef $last_active, $start = 0)
    $struct = DllStructCreate("uint;dword");
    DllStructSetData($struct, 1, DllStructGetSize($struct));
    If $start Then
        DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($struct))
        $last_active = DllStructGetData($struct, 2)
        Return $last_active
    Else
        DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($struct))
        If $last_active <> DllStructGetData($struct, 2) Then
            Local $save = $last_active
            $last_active = DllStructGetData($struct, 2)
            Return $last_active - $save
        EndIf
    EndIf
EndFunc

Func _Terminate()
    Exit
EndFunc

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

Thanks xcal but does anyone know how to correct my last code?

Posted Image

Edited by J0ker

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0