Jump to content

Recommended Posts

Posted

I'm trying to write a script that will close a certain program if the user has had it running, but hasn't used it for some time.

I could write it so it will close the program if it's been minimized or otherwise not 'active' (having focus) for a time, but that won't get the case when they leave the program 'active' when they walk away from their computer.

Is there anyway to tell if a window has gotten any mouse clicks or keypresses recently?

Thanks.

Posted

You're missing my point.

WinActive will only tell you if the window is "active", that is, if it is on top and has focus. If the person is using a program, then gets up and goes home without closing or minimizing it, it will stay "active" all night. I need to know if the person has done anything such as clicked the mouse on or typed something into that program.

Posted

I suppose I could pop up a messagebox or otherwise steal focus from the program every once in a while, and see it they take it back, but that'd annoy them if they are actively using their program.

Of course, it'd hardly be the only program that annoys users by (apparently) randomly stealing their focus...

Posted

will this help you:

Opt("WinTitleMatchMode", 2)

$time = 5000 ;5 sec
$timer = TimerInit()
$title = "Notepad"
run("notepad")

AdlibEnable("AdlibFunc", 10)

func AdlibFunc ()
    if Not WinActive($title) then
        if TimerDiff($timer)>$time Then
            WinClose($title)
            Exit
        EndIf
    Else
        $timer = TimerInit()
    EndIf
EndFunc

While 1
    Sleep(10)
WEndoÝ÷ Ûú®¢×ªº^©oÝ÷ ÚÚºÚ"µÍYÝÚ[XÝ]J   ÌÍÝ]JHÜÚ[XÝ]J ÌÍÝ]JH[

?

That's my random stab at it... I'm not sure what that will do, but hey!

Posted

You can use WinGetText and ControlCommand periodically to examine certain controls in the program to see if there has been any change, such as entering or deleting text in an Edit, change of caret position, or change of selection in a Listbox.

Can you tell us a little more about the program you wish to monitor?

Posted

See if the winactive() then see if the mouse moves by checking MouseGetPos() and see if it matches the same as last time remember to check MouseGetPos[0] and MouseGetPos[1] then you could also Keep running through _ispressed() to see if any key has been pressed, you'd have to generate a list of all the keys to check and it will probably take up a bit of CPU.

Posted (edited)

Try this:

Global $nInactiveTimer = 0, $nLastInput = 0 ;idling timers
Global $nIdlingAllowed = 5000 ;how much time allow idling (miliseconds)
Global $nCheckFrequency = 1000 ;how often check (miliseconds)

;~ ;target app.
Run("notepad")
WinWait("Untitled - Notepad")
$hWin = WinGetHandle("Untitled - Notepad")

;stuct to receive GetLastInputInfo() data.
Global $tagLASTINPUTINFO = DllStructCreate("uint; dword")
DllStructSetData($tagLASTINPUTINFO, 1, 8)
Global $pLASTINPUTINFO = DllStructGetPtr($tagLASTINPUTINFO)

;main loop
While 1
    Sleep($nCheckFrequency)
    If WinActive($hWin) Then
        $nInactiveTimer = 0
        $nLastInput = _GetLastInput()
    Else
        $nInactiveTimer += $nCheckFrequency
    EndIf
    If $nInactiveTimer > $nIdlingAllowed Or $nLastInput > $nIdlingAllowed Then
        MsgBox(0,'FYI', 'Target app has been unused for more than ' & $nIdlingAllowed/1000 & ' seconds.')
        $nInactiveTimer = 0
        $nLastInput = 0
    EndIf
WEnd

; _GetLastInput()
; returns time of user inactivity in miliseconds
Func _GetLastInput()
    If (IsDeclared('pLASTINPUTINFO') <> 1) Or (IsDeclared('tagLASTINPUTINFO') <> 1) Then Return -1
    $aRet = DllCall('user32.dll', 'int', 'GetLastInputInfo', 'ptr', $pLASTINPUTINFO)
    If $aRet[0] = 0 Then Return -1
    $aTick = DllCall('kernel32.dll', 'dword', 'GetTickCount')
    Return $aTick[0] - DllStructGetData($tagLASTINPUTINFO, 2)
EndFunc
Edited by Siao

"be smart, drink your wine"

Posted (edited)

Thanks Siao, that seems to work.

I've never used these dlls before. Where's a good source of info on them?

Edited by jpindar

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
×
×
  • Create New...