How can i make my mouse move in curves?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AHKBeginner333
Posts: 21
Joined: 08 Apr 2022, 04:17

How can i make my mouse move in curves?

Post by AHKBeginner333 » 13 Apr 2022, 14:39

Hello! So im wondering how i can make ahk move the mouse in curves and more like a human instead of in straight lines from a to b everytime. Thank you for help! Much appreciated!
Last edited by gregster on 13 Apr 2022, 14:40, edited 1 time in total.
Reason: Topic moved from 'Scripts and Functions'.

User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: How can i make my mouse move in curves?

Post by mikeyww » 13 Apr 2022, 18:29

If you have a formula, you can simply use it with MouseMove.

There are posted scripts for circles and ellipses available here on the forum.

RussF
Posts: 1264
Joined: 05 Aug 2021, 06:36

Re: How can i make my mouse move in curves?

Post by RussF » 14 Apr 2022, 06:29

Hmmm...trying to fool an "I am not a robot" checkbox? :thumbdown:

Russ

gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: How can i make my mouse move in curves?

Post by gregster » 14 Apr 2022, 06:37

Probably automating a game...

AHKBeginner333
Posts: 21
Joined: 08 Apr 2022, 04:17

Re: How can i make my mouse move in curves?

Post by AHKBeginner333 » 21 Apr 2022, 10:30

mikeyww wrote:
13 Apr 2022, 18:29
If you have a formula, you can simply use it with MouseMove.

There are posted scripts for circles and ellipses available here on the forum.
Hey! Could you possibly give me a link to the thread? Having trouble finding it. Thank you!

User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: How can i make my mouse move in curves?

Post by boiler » 21 Apr 2022, 11:56

Here is one function: MoveMouseInCircle().

User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: How can i make my mouse move in curves?

Post by Hellbent » 21 Apr 2022, 15:56

Vector math works well for this.

Code: Select all

;----------------------------------------------------------------
;----------------------------------------------------------------
;Press NumPad1 to Start
;Press ctrl to Stop
;Press Esc to Exit
;----------------------------------------------------------------
;----------------------------------------------------------------
#SingleInstance,Force
CoordMode,Mouse,Screen
SetBatchLines,-1
Target1:={X:200,Y:200},Target2:={X:700,Y:600},Target3:={X:1350,Y:400}
Loop 3	{
	Gui,%A_Index%:+AlwaysOnTop -Caption
	Gui,%A_Index%:Color,ff0000
	Gui,%A_Index%:Show,% "x" Target%A_Index%.x-10 " y" Target%A_Index%.y-10 " w20 h20"
}
return
Numpad1::
	Acc:=New HB_Vector(Random(0,0),Random(0,0))
	Vel:=New HB_Vector(Random(0,0),Random(0,0))
	Max_VelX:=5,Max_VelY:=5
	Pos:=New HB_Vector(Target3.X,Target3.Y)
	MouseMove,Pos.X,pos.Y
	TVal:=1,count:=0
	While(!GetKeystate("ctrl")){
		count++
		(count=900)?(TVal:=3,Count:=0,Max_VelX:=Random(2,7),Max_VelY:=Random(2,9)):(count=600)?(TVal:=2,Max_VelX:=Random(2,7),Max_VelY:=Random(2,9)):(count=300)?(TVal:=1,Max_VelX:=Random(2,7),Max_VelY:=Random(2,9))
		Acc.X:=Target%TVal%.X,Acc.Y:=Target%TVal%.Y
		Acc.Sub(Pos),Acc.SetMag(.4),Vel.Add(Acc)
		Check_Speed(vel,max_velX,Max_VelY)
		Pos.Add(Vel)
		MouseMove,Pos.X,pos.Y,0
	}
	return
	
Check_Speed(vel,max_velX,Max_VelY){
	(vel.X>max_VelX)?(vel.x:=max_VelX),(vel.x<-max_VelX)?(vel.x:=-max_VelX)
	(vel.y>max_VelY)?(vel.y:=max_VelY),(vel.y<-max_VelY)?(vel.y:=-max_VelY)
}

Random(Min,Max){
	Random,Out,Min,Max
	return Out
}

*Esc::ExitApp

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
	}
}
Use randomly placed gravity attractors to curve the path.

RussF
Posts: 1264
Joined: 05 Aug 2021, 06:36

Re: How can i make my mouse move in curves?

Post by RussF » 22 Apr 2022, 05:51

Hellbent wrote: Use randomly placed gravity attractors to curve the path.
...but would that work without sufficient quantum decelerators to prevent muon crystallization?

:D

Russ

User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: How can i make my mouse move in curves?

Post by Hellbent » 22 Apr 2022, 13:10

RussF wrote:
22 Apr 2022, 05:51


...but would that work without sufficient quantum decelerators to prevent muon crystallization?

:D

Russ
Dammit Russ I'm a doctor not a astrophysicist.


Back on topic:

Here is a little example of using vectors to move between two points.

This shows the paths for a few runs.
20220422140023.png
20220422140023.png (72.36 KiB) Viewed 821 times

Code: Select all


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

Start:=New HB_Vector(100,300)
End:=New HB_Vector(1000,300)

Gui,1:+AlwaysOnTop
Gui,1:Add,Button,xm ym w350 r1 -Theme gSet_Positions,Set New Position
Gui,1:Add,Text,xm y+10 ,Mag1: 
Gui,1:Add,Edit,x+10 w50 r1 vMag1 gSubmitAll,1
Gui,1:Add,Text,xm y+10 ,Mag2: 
Gui,1:Add,Edit,x+10 w50 r1 vMag2 gSubmitAll,6
Gui,1:Show,,Set Start And End Points
gosub,SubmitAll
return
GuiClose:
GuiContextMenu:
*ESC::
	ExitApp

Set_Positions:
	While(!GetKeyState("ctrl"))
		ToolTip,Move your cursor to your START position and then press "ctrl"
	ToolTip,
	Start:=""
	MouseGetPos,x,y
	Start:=New HB_Vector(x,y)
	KeyWait,ctrl
	While(!GetKeyState("ctrl"))
		ToolTip,Move your cursor to your END position and then press "ctrl"
	ToolTip,
	End:=""
	MouseGetPos,x,y
	End:=New HB_Vector(x,y)
	hz:=300
	Loop 3
		SoundBeep,% hz+=100
	return
SubmitAll:
	Gui,1:Submit,NoHide
	return

*Numpad1::
	Pos:=New HB_Vector(Start.X,Start.Y)
	if(abs(Start.X-End.X)>=abs(Start.Y-End.Y)){
		Max_X_Vel:=15,Max_Y_Vel:=8
		New_Vec:=New HB_Vector(Random(End.X-(abs(Start.X-End.X)*.3),End.X+(abs(Start.X-End.X)*.3)),Random(End.Y-(abs(Start.X-End.X)*.1),End.Y+(abs(Start.X-End.X)*.1)))
	}else {
		Max_X_Vel:=8,Max_Y_Vel:=15
		New_Vec:=New HB_Vector(Random(End.X-(abs(Start.X-End.X)*.3),End.X+(abs(Start.X-End.X)*.3)),Random(End.Y-(abs(Start.X-End.X)*.1),End.Y+(abs(Start.X-End.X)*.1)))
	}
	Acc:=New HB_Vector(0,0)
	Vel:=New HB_Vector(0,0)
	MouseMove,Start.X,Start.Y
	CountDown()
	count:=0
	While(Pos.dist(New_Vec)>Random(20,200)){
		Acc.X:=New_Vec.X,Acc.Y:=New_Vec.Y
		Acc.Sub(Pos),Acc.SetMag(Mag1),Vel.Add(Acc)
		Check_Speed(vel,Max_X_Vel,Max_Y_Vel)
		Pos.Add(Vel)
		MouseMove,Pos.X,Pos.Y,0
	}
	Vel.X:=0,Vel.Y:=0,count:=0
	While(Pos.dist(End)>15){
		if(++Count>40)
			Vel.X*=.5,Vel.Y*=.5,count:=0
		Acc.X:=End.X,Acc.Y:=End.Y
		Acc.Sub(Pos),Acc.SetMag(Mag2),Vel.Add(Acc)
		Check_Speed(vel,Max_X_Vel,Max_Y_Vel)
		Pos.Add(Vel)
		MouseMove,Pos.X,Pos.Y,0
	}
	return

Check_Speed(vel,max_velX,Max_VelY){
	(vel.X>max_VelX)?(vel.x:=max_VelX),(vel.x<-max_VelX)?(vel.x:=-max_VelX)
	(vel.y>max_VelY)?(vel.y:=max_VelY),(vel.y<-max_VelY)?(vel.y:=-max_VelY)
}

Random(Min,Max){
	Random,Out,Min,Max
	return Out
}

CountDown(){
	ToolTip,3
	Sleep,300
	ToolTip,2
	Sleep,300
	ToolTip,1
	Sleep,300
	ToolTip,
}




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
	}
}

AHKBeginner333
Posts: 21
Joined: 08 Apr 2022, 04:17

Re: How can i make my mouse move in curves?

Post by AHKBeginner333 » 18 May 2022, 13:35

Hellbent wrote:
22 Apr 2022, 13:10
RussF wrote:
22 Apr 2022, 05:51


...but would that work without sufficient quantum decelerators to prevent muon crystallization?

:D

Russ
Dammit Russ I'm a doctor not a astrophysicist.


Back on topic:

Here is a little example of using vectors to move between two points.

This shows the paths for a few runs.

20220422140023.png

Code: Select all


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

Start:=New HB_Vector(100,300)
End:=New HB_Vector(1000,300)

Gui,1:+AlwaysOnTop
Gui,1:Add,Button,xm ym w350 r1 -Theme gSet_Positions,Set New Position
Gui,1:Add,Text,xm y+10 ,Mag1: 
Gui,1:Add,Edit,x+10 w50 r1 vMag1 gSubmitAll,1
Gui,1:Add,Text,xm y+10 ,Mag2: 
Gui,1:Add,Edit,x+10 w50 r1 vMag2 gSubmitAll,6
Gui,1:Show,,Set Start And End Points
gosub,SubmitAll
return
GuiClose:
GuiContextMenu:
*ESC::
	ExitApp

Set_Positions:
	While(!GetKeyState("ctrl"))
		ToolTip,Move your cursor to your START position and then press "ctrl"
	ToolTip,
	Start:=""
	MouseGetPos,x,y
	Start:=New HB_Vector(x,y)
	KeyWait,ctrl
	While(!GetKeyState("ctrl"))
		ToolTip,Move your cursor to your END position and then press "ctrl"
	ToolTip,
	End:=""
	MouseGetPos,x,y
	End:=New HB_Vector(x,y)
	hz:=300
	Loop 3
		SoundBeep,% hz+=100
	return
SubmitAll:
	Gui,1:Submit,NoHide
	return

*Numpad1::
	Pos:=New HB_Vector(Start.X,Start.Y)
	if(abs(Start.X-End.X)>=abs(Start.Y-End.Y)){
		Max_X_Vel:=15,Max_Y_Vel:=8
		New_Vec:=New HB_Vector(Random(End.X-(abs(Start.X-End.X)*.3),End.X+(abs(Start.X-End.X)*.3)),Random(End.Y-(abs(Start.X-End.X)*.1),End.Y+(abs(Start.X-End.X)*.1)))
	}else {
		Max_X_Vel:=8,Max_Y_Vel:=15
		New_Vec:=New HB_Vector(Random(End.X-(abs(Start.X-End.X)*.3),End.X+(abs(Start.X-End.X)*.3)),Random(End.Y-(abs(Start.X-End.X)*.1),End.Y+(abs(Start.X-End.X)*.1)))
	}
	Acc:=New HB_Vector(0,0)
	Vel:=New HB_Vector(0,0)
	MouseMove,Start.X,Start.Y
	CountDown()
	count:=0
	While(Pos.dist(New_Vec)>Random(20,200)){
		Acc.X:=New_Vec.X,Acc.Y:=New_Vec.Y
		Acc.Sub(Pos),Acc.SetMag(Mag1),Vel.Add(Acc)
		Check_Speed(vel,Max_X_Vel,Max_Y_Vel)
		Pos.Add(Vel)
		MouseMove,Pos.X,Pos.Y,0
	}
	Vel.X:=0,Vel.Y:=0,count:=0
	While(Pos.dist(End)>15){
		if(++Count>40)
			Vel.X*=.5,Vel.Y*=.5,count:=0
		Acc.X:=End.X,Acc.Y:=End.Y
		Acc.Sub(Pos),Acc.SetMag(Mag2),Vel.Add(Acc)
		Check_Speed(vel,Max_X_Vel,Max_Y_Vel)
		Pos.Add(Vel)
		MouseMove,Pos.X,Pos.Y,0
	}
	return

Check_Speed(vel,max_velX,Max_VelY){
	(vel.X>max_VelX)?(vel.x:=max_VelX),(vel.x<-max_VelX)?(vel.x:=-max_VelX)
	(vel.y>max_VelY)?(vel.y:=max_VelY),(vel.y<-max_VelY)?(vel.y:=-max_VelY)
}

Random(Min,Max){
	Random,Out,Min,Max
	return Out
}

CountDown(){
	ToolTip,3
	Sleep,300
	ToolTip,2
	Sleep,300
	ToolTip,1
	Sleep,300
	ToolTip,
}




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
	}
}

Hey, thank you for this it works beautifully but how can i use it in a script where i need my mouse to be moved to specific coordinates with your function? As of now it only moves around these 3 red squares, i need it to move in the same way but to specific coordinates, how do i do that?

Post Reply

Return to “Ask for Help (v1)”