A_TimeIdlePhysical to detect any input but not mouse movement?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
aitha

A_TimeIdlePhysical to detect any input but not mouse movement?

26 Jun 2016, 06:37

I'm trying to detect any form of input otherthan mouse movement so that I can make another script only perform actions when the user hasn't done any input for more than 1sec.

Code: Select all

#NoEnv
#Persistent
#singleInstance, Force
#MaxThreadsPerHotkey, 2

SetTimer, KeyIdle, 100
KeyIdle:
    if(A_TimeIdlePhysical < 1000){
	;Input less than 1sec ago
    } else {
	;Idle
    }
return
The issue with this script is that it includes mouse movement.
So I put #InstallKeybdHook at the top to use it only for keyboard keys, and then added the following below for the mouse clicks:

Code: Select all

~LButton::
;Key Down
KeyWait, LButton
Sleep 1000
;Idle
exit

~RButton::
;Key Down
KeyWait, RButton
Sleep 1000
;Idle
exit

~MButton::
;Key Down
KeyWait, MButton
Sleep 1000
;Idle
exit
But this causes issues with my keyboard handling because it automatically calls the mouse hook so A_TimeIdlePhysical is also detecting mouse movement..

Anyone know a solution to this? Possible to make A_TimeIdlePhysical just ignore mouse movement?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: A_TimeIdlePhysical to detect any input but not mouse movement?

10 Aug 2017, 20:30

Bump.

Variables and Expressions
https://autohotkey.com/docs/Variables.htm#TimeIdle

I had the same issue. I would suppose that the simplest solution would be to have another script, that sends a message to this script every time a mouse button is pressed. That would then either reset a 'mouse idle time' variable or somehow cause A_TimeIdlePhysical to be reset to 0.

I would be interested in any ideas.

Also any links re. using mouse gestures, or indeed any mouse movement, to trigger a hotkey would be most welcome.

In general, I think AutoHotkey + mouse movement, needs greater consideration. Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: A_TimeIdlePhysical to detect any input but not mouse movement?

10 Aug 2017, 22:11

Concerning OP:

Code: Select all

#singleInstance force
#persistent
#include *i <Lib_1>

setTimer,idleCheck,10
setTimer,tool,10
return

tool:
;tool(getTimeIdleNoMouse())
tooltip % getTimeIdleNoMouse()
return

idleCheck(){
    global _idleTick
    if(!_idleTick)
        _idleTick:=a_tickCount
    input,c,L1VMI,{backspace}{LControl}{RControl}{LShift}{RShift}{Appskey}{Up}{Down}{Left}{Right}{Capslock}{Printscreen}{home}{end}{pgup}{pgdn}{LAlt}{RAlt}{ins}{del}{pause}{break}
    _idleTick:=a_tickCount
}

getTimeIdleNoMouse(){
    global _idleTick
    return a_tickCount - _idleTick
}

~LButton::
~RButton::
~MButton::
~WheelUp::
~WheelDown::
~XButton1::
~XButton2::
_idleTick:=a_tickCount
return
This isn't perfect, certainly. It won't get key-up events, so if you were to say, click and drag something, it would only reset upon the first key-down event. I also didn't add all keys to the endkeys list that could be there, such as media keys. And, it's a bit intrusive, not being confined to a single function. You COULD get away with two functions: the first being idleCheck(), and the second being a reference for all the hotkeys (to do nothing). However, you could double-up on the second function, allowing an optional parameter. If the parameter has content, it could return the idleTime. Somewhere though, you'd have to loop through all the hotkeys and set them, possibly in a similar fashion to the if-statement in idleCheck().

For it to act like A_TimeIdle rather than A_TimeIdlePhysical, remove the I option for the input command.

Edit:

Code: Select all

#singleInstance force
#persistent
#include *i <Lib_1>
#hotkeyInterval 200 ; necessary for hyper-scrolling wheels

setTimer,idleCheck,10
setTimer,tool,10
return

tool:
;tool(getTimeIdleNoMouse())
tooltip % getTimeIdleNoMouse(1)
return

idleCheck(){
    hotkeylist:=["LButton","RButton","MButton","WheelUp","WheelDown","XButton1","XButton2"]
    global _idleTick

    if(!_idleTick){
        _idleTick:=a_tickCount
        for i,a in hotkeylist
            hotkey,% "~*" . a,getTimeIdleNoMouse
    }
    input,c,L1VMI,{backspace}{LControl}{RControl}{LShift}{RShift}{Appskey}{Up}{Down}{Left}{Right}{Capslock}{Printscreen}{home}{end}{pgup}{pgdn}{LAlt}{RAlt}{ins}{del}{pause}{break}
    _idleTick:=a_tickCount
}

getTimeIdleNoMouse(bool:=0){
    global _idleTick

    if(!bool)
        _idleTick:=a_tickCount
    else
        return a_tickCount - _idleTick
}
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: A_TimeIdlePhysical to detect any input but not mouse movement?

11 Aug 2017, 02:15

You have already recieved good advice. Here is another method, using a mouse hook, very briefly tested.

Code: Select all

#InstallKeybdHook
global mouseClicked:=A_TickCount
global threshold:=1000
mh:=new mouseHook(func("g"))
mh.hook()

SetTimer,checkIdel, 100

esc::exitapp

g(flags,x,y){
	static px,py
	if (px==x && py==y)
		mouseClicked:=A_TickCount
	px:=x,py:=y
}

checkIdel(){
	if (A_TickCount-mouseClicked>threshold && A_TimeIdlePhysical>threshold)
		ToolTip, % "Idel @ " A_TickCount
}
	

class mouseHook{
	; User methods
	hook(){
		if !this.hHook												; WH_MOUSE_LL := 14									  hMod:=0	 dwThreadId:=0
			this.hHook:=DllCall("User32.dll\SetWindowsHookEx", "Int", 14, "Ptr", this.regCallBack:=RegisterCallback(this.LowLevelMouseProc,"",4, &this), "Uint", 0, "Uint", 0, "Ptr")
		return
	}
	unHook(){
		if this.hHook
			DllCall("User32.dll\UnhookWindowsHookEx", "Uint", this.hHook)
		return this.hHook:=""
	}
	; Internal methods.
	__new(callbackFunc){
		this.callbackFunc:=callbackFunc
	}
	LowLevelMouseProc(args*) {  
		; (nCode, wParam, lParam)
		Critical
		this:=Object(A_EventInfo),nCode:=NumGet(args-A_PtrSize,"Int"), wParam:=NumGet(args+0,0,"Ptr"), lParam:=NumGet(args+0,A_PtrSize,"UPtr") 
		x:=NumGet(lParam+0,0,"Int"),y:=NumGet(lParam+0,4,"Int"),flags:=NumGet(lParam+0,12,"UInt")
		this.callbackFunc.call(flags,x,y)
		return DllCall("User32.dll\CallNextHookEx", "Uint",0, "Int", nCode,"Uptr", wParam,"Ptr",lParam)
	}
	__Delete(){  
		this.unHook()
		if this.regCallBack
			DllCall("GlobalFree", "Ptr", this.regCallBack)
		return 
	}
}

Masonjar13 wrote:And, it's a bit intrusive, not being confined to a single function.
I do not agree :mrgreen:
jeeswg wrote: any mouse movement, to trigger a hotkey would be most welcome.
There is some content here: Custom hotkey for mouse movement?

Cheers.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: A_TimeIdlePhysical to detect any input but not mouse movement?

13 Sep 2017, 02:30

Two scripts for muting the speakers while typing/clicking, mouse movement is ignored.

Code: Select all

;mute speakers while typing/clicking
;speakers on if no keyboard presses/mouse clicks for 3 seconds or more
;mouse movement is ignored

#InstallKeybdHook ;A_TimeIdlePhysical ignores mouse clicks/mouse moves
;#InstallMouseHook ;A_TimeIdlePhysical ignores key presses
;#UseHook
;#UseHook Off

SoundGet, vIsMuteOnExit,, Mute
vIsMuteOnExit := "Off" ;state to set when close script
OnMessage(0x5555, "MsgMonitor")
OnExit("ExitFunc")

vScript = ;continuation section
(
DetectHiddenWindows, On
~*LButton::
~*RButton::
PostMessage, 0x5555,,,, `% "ahk_id " %A_ScriptHwnd%
if !WinExist("ahk_id " %A_ScriptHwnd%)
	ExitApp
return
)

;based on ExecScript example:
;Run / RunWait
;https://autohotkey.com/docs/commands/Run.htm#Examples
oShell := ComObjCreate("WScript.Shell")
oExec := oShell.Exec(A_AhkPath " /ErrorStdOut *")
oExec.StdIn.Write(vScript)
oExec.StdIn.Close()
oShell := oExec := ""

vLastMove := A_TickCount
Loop
{
	vTimeIdle := A_TickCount-vLastMove
	if (A_TimeIdlePhysical > 3000) && (vTimeIdle > 3000) && vIsMute
		SoundSet, % vIsMute := 0,, Mute
	if ((A_TimeIdlePhysical < 3000) || (vTimeIdle < 3000)) && !vIsMute
		SoundSet, % vIsMute := 1,, Mute
	Sleep 100
	;ToolTip, % vTimeIdle " " A_TimeIdle " " A_TimeIdlePhysical
	ToolTip, % vTimeIdle " " A_TimeIdlePhysical
}
return

MsgMonitor(wParam, lParam, uMsg, hWnd)
{
	global vLastMove
	vLastMove := A_TickCount
}

ExitFunc()
{
	global vIsMuteOnExit
	SoundSet, % vIsMuteOnExit,, Mute
}

Code: Select all

;mute speakers while typing/clicking
;speakers on if no keyboard presses/mouse clicks for 3 seconds or more
;mouse movement is ignored

SoundGet, vIsMuteOnExit,, Mute
vIsMuteOnExit := "Off" ;state to set when close script
OnMessage(0x5555, "MsgMonitor")
OnExit("ExitFunc")

vLastMove := A_TickCount
Loop
{
	vTimeIdle := A_TickCount-vLastMove
	if (vTimeIdle > 3000) && vIsMute
		SoundSet, % vIsMute := 0,, Mute
	if (vTimeIdle < 3000) && !vIsMute
		SoundSet, % vIsMute := 1,, Mute
	Sleep 100
	ToolTip, % vTimeIdle " " A_TimeIdle " " A_TimeIdlePhysical
}
return

;add more hotkeys as appropriate
~q::
~w::
~e::
~*LButton::
~*RButton::
vLastMove := A_TickCount
return

ExitFunc()
{
	global vIsMuteOnExit
	SoundSet, % vIsMuteOnExit,, Mute
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
scatrmind
Posts: 8
Joined: 22 Mar 2021, 11:51

Re: A_TimeIdlePhysical to detect any input but not mouse movement?

14 May 2021, 09:22

[EDIT: Added the missing space before "Up" in the Hotkeys]

I know this is not elegant, but below is what I do. I tried some of evilC's classes for looping and creating hotkeys on the fly, but I kept getting errors that disallowed the script from running (operator error perhaps). Making the list of Hotkeys was easy; I copied the list of keys to Excel, added the number keys and alphabet keys to that Excel list, added the syntax in adjacent cells on the first row, populated the remaining rows using the fill handle, merged the keys and syntax (=A1&B1&C1), and pasted the results as text.

Code: Select all

;Set to a time greater than the desired idle time at the beginning of the script
IdleTime:=301

;Use throughout the script where needed. The modifier keys below are monitored because I often hold them for longer than my desired idle time, but I don't want the script to resume if I am holding them.
While ((A_TickCount-IdleTime)<300) || GetKeyState("Shift") || GetKeyState("Ctrl") || GetKeyState("Alt") || GetKeyState("LWin") || GetKeyState("RWin") || GetKeyState("LButton") || GetKeyState("RButton") || GetKeyState("MButton")
	Sleep, 50
;do stuff here

;Place the Hotkeys at the bottom of the script (I removed WheelDown, WheelUp, WheelLeft, and WheelRight). All of these key-down and key-up events trigger... IdleTime:=A_TickCount
~*LButton::
~*RButton::
~*MButton::
~*XButton1::
~*XButton2::
~*CapsLock::
~*Space::
~*Tab::
~*Enter::
~*Return::
~*Escape::
~*Backspace::
~*ScrollLock::
~*Delete::
~*Insert::
~*Home::
~*End::
~*PgUp::
~*PgDn::
~*Up::
~*Down::
~*Left::
~*Right::
~*Numpad0::
~*Numpad1::
~*Numpad2::
~*Numpad3::
~*Numpad4::
~*Numpad5::
~*Numpad6::
~*Numpad7::
~*Numpad8::
~*Numpad9::
~*NumpadDot::
~*NumpadIns::
~*NumpadEnd::
~*NumpadDown::
~*NumpadPgDn::
~*NumpadLeft::
~*NumpadClear::
~*NumpadRight::
~*NumpadHome::
~*NumpadUp::
~*NumpadPgUp::
~*NumpadDel::
~*NumLock::
~*NumpadDiv::
~*NumpadMult::
~*NumpadAdd::
~*NumpadSub::
~*NumpadEnter::
~*F1::
~*F2::
~*F3::
~*F4::
~*F5::
~*F6::
~*F7::
~*F8::
~*F9::
~*F10::
~*F11::
~*F12::
~*F13::
~*F14::
~*F15::
~*F16::
~*F17::
~*F18::
~*F19::
~*F20::
~*F21::
~*F22::
~*F23::
~*F24::
~*LWin::
~*RWin::
~*Control::
~*Alt::
~*Shift::
~*Lcontrol::
~*Rcontrol::
~*LShift::
~*RShift::
~*LAlt::
~*RAlt::
~*Browser_Back::
~*Browser_Forward::
~*Browser_Refresh::
~*Browser_Stop::
~*Browser_Search::
~*Browser_Favorites::
~*Browser_Home::
~*Volume_Mute::
~*Volume_Down::
~*Volume_Up::
~*Media_Next::
~*Media_Prev::
~*Media_Stop::
~*Media_Play_Pause::
~*Launch_Mail::
~*Launch_Media::
~*Launch_App1::
~*Launch_App2::
~*AppsKey::
~*PrintScreen::
~*CtrlBreak::
~*Pause::
~*Break::
~*Help::
~*Sleep::
~*`::
~*1::
~*2::
~*3::
~*4::
~*5::
~*6::
~*7::
~*8::
~*9::
~*0::
~*-::
~*=::
~*q::
~*w::
~*e::
~*r::
~*t::
~*y::
~*u::
~*i::
~*o::
~*p::
~*[::
~*]::
~*\::
~*a::
~*s::
~*d::
~*f::
~*g::
~*h::
~*j::
~*k::
~*l::
~*;::
~*'::
~*z::
~*x::
~*c::
~*v::
~*b::
~*n::
~*m::
~*,::
~*.::
~*/::
~*LButton Up::
~*RButton Up::
~*MButton Up::
~*XButton1 Up::
~*XButton2 Up::
~*CapsLock Up::
~*Space Up::
~*Tab Up::
~*Enter Up::
~*Return Up::
~*Escape Up::
~*Backspace Up::
~*ScrollLock Up::
~*Delete Up::
~*Insert Up::
~*Home Up::
~*End Up::
~*PgUp Up::
~*PgDn Up::
~*Up Up::
~*Down Up::
~*Left Up::
~*Right Up::
~*Numpad0 Up::
~*Numpad1 Up::
~*Numpad2 Up::
~*Numpad3 Up::
~*Numpad4 Up::
~*Numpad5 Up::
~*Numpad6 Up::
~*Numpad7 Up::
~*Numpad8 Up::
~*Numpad9 Up::
~*NumpadDot Up::
~*NumpadIns Up::
~*NumpadEnd Up::
~*NumpadDown Up::
~*NumpadPgDn Up::
~*NumpadLeft Up::
~*NumpadClear Up::
~*NumpadRight Up::
~*NumpadHome Up::
~*NumpadUp Up::
~*NumpadPgUp Up::
~*NumpadDel Up::
~*NumLock Up::
~*NumpadDiv Up::
~*NumpadMult Up::
~*NumpadAdd Up::
~*NumpadSub Up::
~*NumpadEnter Up::
~*F1 Up::
~*F2 Up::
~*F3 Up::
~*F4 Up::
~*F5 Up::
~*F6 Up::
~*F7 Up::
~*F8 Up::
~*F9 Up::
~*F10 Up::
~*F11 Up::
~*F12 Up::
~*F13 Up::
~*F14 Up::
~*F15 Up::
~*F16 Up::
~*F17 Up::
~*F18 Up::
~*F19 Up::
~*F20 Up::
~*F21 Up::
~*F22 Up::
~*F23 Up::
~*F24 Up::
~*LWin Up::
~*RWin Up::
~*Control Up::
~*Alt Up::
~*Shift Up::
~*Lcontrol Up::
~*Rcontrol Up::
~*LShift Up::
~*RShift Up::
~*LAlt Up::
~*RAlt Up::
~*Browser_Back Up::
~*Browser_Forward Up::
~*Browser_Refresh Up::
~*Browser_Stop Up::
~*Browser_Search Up::
~*Browser_Favorites Up::
~*Browser_Home Up::
~*Volume_Mute Up::
~*Volume_Down Up::
~*Volume_Up Up::
~*Media_Next Up::
~*Media_Prev Up::
~*Media_Stop Up::
~*Media_Play_Pause Up::
~*Launch_Mail Up::
~*Launch_Media Up::
~*Launch_App1 Up::
~*Launch_App2 Up::
~*AppsKey Up::
~*PrintScreen Up::
~*CtrlBreak Up::
~*Pause Up::
~*Break Up::
~*Help Up::
~*Sleep Up::
~*` Up::
~*1 Up::
~*2 Up::
~*3 Up::
~*4 Up::
~*5 Up::
~*6 Up::
~*7 Up::
~*8 Up::
~*9 Up::
~*0 Up::
~*- Up::
~*= Up::
~*q Up::
~*w Up::
~*e Up::
~*r Up::
~*t Up::
~*y Up::
~*u Up::
~*i Up::
~*o Up::
~*p Up::
~*[ Up::
~*] Up::
~*\ Up::
~*a Up::
~*s Up::
~*d Up::
~*f Up::
~*g Up::
~*h Up::
~*j Up::
~*k Up::
~*l Up::
~*; Up::
~*' Up::
~*z Up::
~*x Up::
~*c Up::
~*v Up::
~*b Up::
~*n Up::
~*m Up::
~*, Up::
~*. Up::
~*/ Up::
IdleTime:=A_TickCount
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: anselmopimiento1996, JoeWinograd, peter_ahk and 214 guests