Trigger a script when the mouse stops moving

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Trigger a script when the mouse stops moving

21 Oct 2016, 19:23

Hello,

Rather than using a hotkey, is it possible to launch a script by moving then stopping the mouse?

Thanks
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
Garbidge_1
Posts: 14
Joined: 02 Sep 2014, 23:03

Re: Trigger a script when the mouse stops moving

22 Oct 2016, 07:43

Code: Select all

#NoEnv
Do_ToolTip:=1
mWait:=200
gosub Mouse_Check
return  ; end auto execute

MOUSE_STOPPED:
	;
	; put your code here
	;
	return

!Esc::       ; Alt + Esc to quit
	ExitApp

Mouse_Check:
	mStill:=0
	MouseGetPos, Ox, Oy
	Sleep %mWait%
	loop {
		MouseGetPos, Mx, My
		if( Mx = Ox && My = Oy ) {
			mStill++
			if( mStill >= 5 ) {   ; 5 = 1000/mWait 
				mStill:=0
				if( !IS_MSTOPPED && Do_ToolTip )
					ToolTip, Mouse has stopped moving.
				IS_MSTOPPED:=1
				SetTimer, MOUSE_STOPPED, -1
			}
		} else  {
			IS_MSTOPPED:=0
			ToolTip
		}
		Ox:=Mx, Oy:=My
		Sleep %mWait%
	}
	return
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Trigger a script when the mouse stops moving

24 Oct 2016, 12:23

The technique posted by Garbidge_1 will not detect when the mouse stops moving, it will detect when the mouse cursor stops moving. You could be moving the mouse against the edge of the monitor, and the cursor would not move, but the mouse would still be moving.
Also, in many games, the windows cursor will not move when you move the mouse, so this code will not do anything.

Detecting when the mouse itself stops moving is non-trivial, because you simply stop getting mouse move messages when it stops moving, so you need to set a timer running after every move, and when the timer fires, you consider the mouse stopped.

If you want to detect the mouse stopping moving, see the sample script in my MouseDelta library - it shows how to do this with the RAWINPUT API. All the code you need is there.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: alawsareps, Google [Bot], mebelantikjaya, mikeyww, RussF and 309 guests