gcue 8 Posted March 20, 2017 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! Share this post Link to post Share on other sites
Xandy 380 Posted March 20, 2017 (edited) Your topic is interesting! Check for clicks, check for keys, and check if your script gui is active? Edited March 20, 2017 by Xandy (-_-) Xandy About (^o^) Discord - Xandy Programmer MapIt (Tile world editor, Image Tile Extractor, and Game Maker) Share this post Link to post Share on other sites
Xandy 380 Posted March 20, 2017 (edited) expandcollapse popup#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 March 20, 2017 by Xandy (-_-) Xandy About (^o^) Discord - Xandy Programmer MapIt (Tile world editor, Image Tile Extractor, and Game Maker) Share this post Link to post Share on other sites
gcue 8 Posted March 20, 2017 good call on checking if gui is active - didnt think of that thanks for the example too! ill play with it and let you know thanks again Share this post Link to post Share on other sites
InunoTaishou 184 Posted March 20, 2017 (edited) expandcollapse popup#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 March 20, 2017 by InunoTaishou Should have been >= 5 in FunctionA() 2 Share this post Link to post Share on other sites