A_TimeIdlePhysical to detect any input but not mouse movement?

Get help with using AutoHotkey 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: 6819
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: 1478
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

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 7 Ultimate | Editor: Notepad++
My Personal Function Library | Check Out My Computer Rig
Helgef
Posts: 3981
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: 6819
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

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Bort P, flyingDman and 213 guests