Better Mouse Move? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
trhaynes
Posts: 7
Joined: 05 Jan 2021, 11:52

Better Mouse Move?

05 Jan 2021, 11:54

Hi all

When I use MouseMove, the pointer moves *close* to where it's supposed to end up, then will do a 50 or 60 pixel horizontal correction to finally get there.

Is there a way to have the mouse move to the exact coordinates, without needing that bit of corrective jogging at the end?

Thanks!
User avatar
Xtra
Posts: 2048
Joined: 02 Oct 2015, 12:15

Re: Better Mouse Move?

05 Jan 2021, 12:14

MouseMove, X, Y, 0
Use 0 for the speed parameter. See: MouseMove
User avatar
Chunjee
Posts: 780
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: Better Mouse Move?

05 Jan 2021, 13:12

a speed of 0 will instantly move it; eliminating the unwanted final movements
trhaynes
Posts: 7
Joined: 05 Jan 2021, 11:52

Re: Better Mouse Move?

05 Jan 2021, 14:47

I'm doing a training video where the mouse is moved by ahk script (rather than my own shaky and inconsistent hands), so instantaneous movement is not desired. What I'd like is controllable speed and have it reach the final destination without horizontal or vertical corrections as the end.

Any other ideas...? Thank you!
User avatar
mikeyww
Posts: 5104
Joined: 09 Sep 2014, 18:38

Re: Better Mouse Move?

05 Jan 2021, 14:49

A speed of 10 seems to work well.
trhaynes
Posts: 7
Joined: 05 Jan 2021, 11:52

Re: Better Mouse Move?

05 Jan 2021, 14:51

mikeyww wrote:
05 Jan 2021, 14:49
A speed of 10 seems to work well.
It's accurate, but too fast for a training video. :cry:
User avatar
Hellbent
Posts: 1181
Joined: 23 Sep 2017, 13:34

Re: Better Mouse Move?  Topic is solved

05 Jan 2021, 18:23

Give this a try.

Code: Select all

#SingleInstance, Force
SetBatchLines, -1
CoordMode, Mouse, Screen
return

*ESC::ExitApp

*Numpad1::
	MousePosition := "", MousePosition := New HB_Vector(100,A_ScreenHeight/2)  ; The starting position for your cursor  [ Set a x and y value (x,y)].
	MouseTarget := "", MouseTarget := New HB_Vector(A_ScreenWidth-100,100)	; The ending position for you cursor [ Set a x and y value (x,y)].
	Acc := "", Acc := New HB_Vector()
	Speed := 5 ; <<<<<<--------------------  Change Speed to move faster (higher value) or slower (lower value).
	Dist := Floor(MousePosition.Dist(MouseTarget)/Speed)
	DllCall("SetCursorPos", "int", MousePosition.X, "int", MousePosition.Y)
	While(--Dist)	{
		Acc.X := MouseTarget.X, Acc.Y := MouseTarget.Y
		Acc.Sub(MousePosition)
		Acc.SetMag(Speed)
		MousePosition.Add(Acc)
		DllCall("SetCursorPos", "int", MousePosition.X, "int", MousePosition.Y)
		sleep, 10
	}
	DllCall("SetCursorPos", "int", MouseTarget.X, "int", MouseTarget.Y)
	return

; Vector class (not required but makes things easy).
;*******************************************************************************************************************************************************
;*******************************************************************************************************************************************************
;*******************************************************************************************************************************************************
Class HB_Vector	{
	__New(x:=0,y:=0){
		This.X:=x
		This.Y:=y
	}
	Add(Other_HB_Vector){
		This.X+=Other_HB_Vector.X
		This.Y+=Other_HB_Vector.Y
	}
	Sub(Other_HB_Vector){
		This.X-=Other_HB_Vector.X
		This.Y-=Other_HB_Vector.Y
	}
	mag(){
		return Sqrt(This.X*This.X + This.Y*This.Y)
	}
	magsq(){
		return This.Mag()**2
	}	
	setMag(in1){
		m:=This.Mag()
		This.X := This.X * in1/m
		This.Y := This.Y * in1/m
		return This
	}
	mult(in1,in2:="",in3:="",in4:="",in5:=""){
		if(IsObject(in1)&&in2=""){
			This.X*=In1.X 
			This.Y*=In1.Y 
		}else if(!IsObject(In1)&&In2=""){
			This.X*=In1
			This.Y*=In1
		}else if(!IsObject(In1)&&IsObject(In2)){
			This.X*=In1*In2.X
			This.Y*=In1*In2.Y
		}else if(IsObject(In1)&&IsObject(In2)){
			This.X*=In1.X*In2.X
			This.Y*=In1.Y*In2.Y
		}	
	}
	div(in1,in2:="",in3:="",in4:="",in5:=""){
		if(IsObject(in1)&&in2=""){
			This.X/=In1.X 
			This.Y/=In1.Y 
		}else if(!IsObject(In1)&&In2=""){
			This.X/=In1
			This.Y/=In1
		}else if(!IsObject(In1)&&IsObject(In2)){
			This.X/=In1/In2.X
			This.Y/=In1/In2.Y
		}else if(IsObject(In1)&&IsObject(In2)){
			This.X/=In1.X/In2.X
			This.Y/=In1.Y/In2.Y
		}	
	}
	dist(in1){
		return Sqrt(((This.X-In1.X)**2) + ((This.Y-In1.Y)**2))
	}
	dot(in1){
		return (This.X*in1.X)+(This.Y*In1.Y)
	}
	cross(in1){
		return This.X*In1.Y-This.Y*In1.X
	}
	Norm(){
		m:=This.Mag()
		This.X/=m
		This.Y/=m
	}
}
trhaynes
Posts: 7
Joined: 05 Jan 2021, 11:52

Re: Better Mouse Move?

06 Jan 2021, 10:00

Thank you, @Hellbent ! This is perfect, and does exactly what I need. :superhappy: You saved me a ton of work!!

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], boiler, Google [Bot], LeonAskin, wineguy, yoooooo and 55 guests