Jump to content

"if ($nTimer >= 100) Then" user idle problem


Recommended Posts

i am trying to make a script that will wait for me to be idle then start, and if the i do something it will stop then wait for me to be idle again. i want to have the computer doing something constantly.

the first function waits for me to be idle successfully, but once the script starts running, it doesnt stop until i exit. what i tried to do is take the first function and reverse it (i found it in an old help section in the forum), but i dont understand part of the function:

;these are the vartiables
Global $OldMousePosition = MouseGetPos()
$NewMousePosition = MouseGetPos()
;this is what i dont understand
if (($NewMousePosition[0] == $OldMousePosition[0]) and ($NewMousePosition[1] == $OldMousePosition[1])) Then

ive tried the help section, and google to find an example of "stop when the user does something", and i know about _Timer_GetIdleTime() but i cant figure out how to apply that here. (_Timer_GetIdleTime() only recognizes key strokes?)

im not looking for somebody to code for me, i really want to figure this out. help file/function i missed?

here is my code:

#include <timers.au3>
AdLibEnable("_UpdateTimer", 100)

Global $nTimer = 0
Global $OldMousePosition = MouseGetPos()
GLobal $NewMousePosition
Global $Time = _Timer_GetIdleTime()

While(1)
    sleep(10)
WEnd

;\/ if they dont move, then start
Func _UpdateTimer()
    $nTimer += 100 ;100 ms
    
    $NewMousePosition = MouseGetPos()
    if (($NewMousePosition[0] == $OldMousePosition[0]) and ($NewMousePosition[1] == $OldMousePosition[1])) Then
        ; The user is still idle (you might also want to check for key presses
    Else
        ; The user moved, we should start the timer over.
        $OldMousePosition = $NewMousePosition
        $nTimer = 0
    EndIf
    
    if ($nTimer >= 5000) Then ;Change the '5000' to the number in milliseconds you want to wait, with your user being idle.
        _OnTimerFinish()
        $nTimer = 0
    EndIf
EndFunc

Func _OnTimerFinish()
    $nTimer += 100 ;100 ms
    
    $NewMousePosition = MouseGetPos()
    if (($NewMousePosition[0] == $OldMousePosition[0]) and ($NewMousePosition[1] == $OldMousePosition[1])) Then
        ; The user is still idle (you might also want to check for key presses
    Else
        ; The user moved, we should start the timer over.
        $OldMousePosition = $NewMousePosition
        $nTimer = 0
    EndIf
    
    if ($nTimer >= 100) Then 
            
            While sleep (3000)
                ;various different scripts here
                if ($OldMousePosition = $NewMousePosition) Then ExitLoop 
                    _UpdateTimer()
            wend 
         $nTimer = 0
    EndIf
EndFunc

EDIT:

this is where i got the original help:

http://www.autoitscript.com/forum/index.php?showtopic=66087&st=20

Edited by RogerRabbitsClone
<--a good way to start you day
Link to comment
Share on other sites

i have a few different scripts written for different computers. one of them is a loop that renames files in a specific order and drops them to a folder in the server at my job. and another deletes files from the desktop of customer computers. there are allot of students that come into my store and just leave things like rental applications and scaned passports on the desktops of our computers. so i have a script that selects things on the desktop and our servers scans folder and deletes them after a certain amount of time.

<--a good way to start you day
Link to comment
Share on other sites

i messed with _Timer_GetIdleTime() it works very well determining idle time for mouse movements or keystrokes heres a small smiple example just to see how it works

#Include <Timers.au3>

While 1
     sleep(10)
     $idle = _Timer_GetIdleTime()
     ToolTip($idle)
WEnd

just sits and idles around puts a tooltip the the idle time in ms next to the mouse

you can see what happens when you move the mouse and type on the keyboard ect...

Link to comment
Share on other sites

so in theory, this should keep opening notepad until i move the mouse?

#Include <Timers.au3>

Global $Time = _Timer_GetIdleTime()
while run ("notepad.exe")
sleep (3000)
;
if $time > 100 then exitloop
wend

the one above wouldn't really work but i thought of this one

this will open Notepad after 10 seconds of being idle and then close it if you move the mouse then if your idle another 10 seconds it'll open it again

#include <Timers.au3>
while 1
    $Timer = _Timer_GetIdleTime()
    if $Timer = 10000 Then
        $pid = Run("Notepad.exe")
        Do
            $Timer = _Timer_GetIdleTime()
        Until $Timer < 10000
        ProcessClose($pid)
    EndIf
WEnd
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...