Jump to content

detect keyboard/mouse activity and inactivity within script


gcue
 Share

Recommended Posts

hello

i am trying to detect user keyboard/mouse inactivity within script (not inactivty in windows just within script) to run a Function A - but if user tries to use script again by moving mouse or using keyboard within script - exit Function A

I see lots of scripts for _Timer_GetIdleTime, but that detects idle time for windows - not script.

thanks in advance!

Link to comment
Share on other sites

#include <GUIConstantsEx.au3>
$time_inactive = 0
$time_inactive_len = 1000 * 5

$state_changed = 0; This just avoids label flashing

$mehGui = guicreate("mehGui", 320, 200)
$idLabel_state = GUICtrlCreateLabel("Active", 10, 10, 200, 20)
GUISetState()

do
    $msg = GUIGetMsg()
    if WinActive($mehGui) Then
        $event = getkey()
        $event += getmousemove($mehGui)
        if $event > 0 Then
            $time_inactive = TimerInit()
            if $state_changed = 1 then
                GUICtrlSetData($idLabel_state, "Active")
                $state_changed = 0
            endif
        endif
    endif; WinActive($mehGui)

    if TimerDiff($time_inactive) >= $time_inactive_len Then
        if $state_changed = 0 then
            GUICtrlSetData($idLabel_state, "Inactive")
            $state_changed = 1
        endif
    endif
until $msg = $gui_event_close

func getkey()

EndFunc

func getmousemove($hGui)
    static $aMousePoLast = [0, 0]
    $aWinPo = WinGetPos($hGui)
    if @error <> 0 then return
    $aMousePo = MouseGetPos()
    if $aMousePoLast[0] = $aMousePo[0] and $aMousePoLast[1] = $aMousePo[1] Then return 0
    if $aMousePo[0] >= $aWinPo[0] and $aMousePo[0] <= $aWinPo[0] + $aWinPo[2] then
        if $aMousePo[1] >= $aWinPo[1] and $aMousePo[1] <= $aWinPo[1] + $aWinPo[3] then
            $aMousePoLast[0] = $aMousePo[0]
            $aMousePoLast[1] = $aMousePo[1]
            Return 1
        endif
    endif
EndFunc

[Updated: function name: getmousemove() and no longer reports Active when mouse is idle over window.

Above script checks mouse moves over window while window has focus

I didn't include a getkey() definition b/c I don't know how to check for any key down.  Well no.. I can do it but I am not allowed to share.  I'd love to see how you do it so I can share my scripts again.  

Edited by Xandy
Link to comment
Share on other sites

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>

Global $hMain = GUICreate("Test", 400, 400)
Global $iActiveTimer = TimerInit()

GUIRegisterMsg($WM_MOUSEMOVE, WM_MOUSEMOVE)
GUIRegisterMsg($WM_KEYDOWN, Active)
GUIRegisterMsg($WM_KEYUP, Active)
GUIRegisterMsg($WM_LBUTTONDOWN, Active)
GUIRegisterMsg($WM_LBUTTONUP, Active)
GUIRegisterMsg($WM_RBUTTONDOWN, Active)
GUIRegisterMsg($WM_RBUTTONUP, Active)
GUIRegisterMsg($WM_MBUTTONDOWN, Active)
GUIRegisterMsg($WM_MBUTTONUP, Active)

GUISetState(@SW_SHOW, $hMain)

While (True)
    Switch (GUIGetMsg())
        Case $GUI_EVENT_CLOSE
            Exit 0
        Case Else
            ToolTip("Inactive for " & Round(TimerDiff($iActiveTimer) / 1000, 0) & "s")
            If (Round(TimerDiff($iActiveTimer) / 1000, 0) >= 5) Then FunctionA()
    EndSwitch
WEnd

Func FunctionA()
    While (Round(TimerDiff($iActiveTimer) / 1000, 0) >= 5)
        ToolTip("FunctionA()")
        Sleep(100)
    WEnd
    ToolTip("")
EndFunc

Func Active($hWnd, $iMsg, $wParam, $lParam)
    $iActiveTimer = TimerInit()
EndFunc

Func WM_MOUSEMOVE($hWnd, $iMsg, $wParam, $lParam)
    Local Static $aLastPos[2]
    Local $iX = _WinAPI_LoWord($lParam)
    Local $iY = _WinAPI_HiWord($lParam)

    If ($iX <> $aLastPos[0] or $iY <> $aLastPos[1]) Then
        $iActiveTimer = TimerInit()
        $aLastPos[0] = $iX
        $aLastPos[1] = $iY
    EndIf
EndFunc

Give that a go.

Edited by InunoTaishou
Should have been >= 5 in FunctionA()
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...