Smooth mousemove / random movement to specified x/y cords

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
isuckatahk
Posts: 36
Joined: 12 Mar 2020, 02:56

Smooth mousemove / random movement to specified x/y cords

17 Jan 2021, 23:05

Hey I'm not really good with AHK so figured I would ask for help. I'm trying to make a script that moves to a specified X/Y and clicks within that range randomly this is what I have so far.

When I press F7 it moves to a random x/y coordinate that I have specified in clicks but it does it way too fast and I want to slow it down but when I add sleep or delay it's jittery/choppy and I want it to move smoothly like a person.

Code: Select all

f7:: ;/Trigger key

MouseGetPos, x, y
SetDefaultMouseSpeed, 1

;Cord 1
random, a, 992,1023
random, b, 520,535
Click, Left, %a%, %b%

;Cord 2
random, c, 1049,1071
random, d, 520,535
Click, Left, %c%, %d%

;Cord 3
random, e, 1098,1124
random, f, 520,535
Click, Left, %e%, %f%

return
[Mod edit: [code][/code] tags added.]
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Smooth mousemove / random movement to specified x/y cords

18 Jan 2021, 00:48

Press Numpad1 a few times.
Try Changing the speed and run again.

Code: Select all

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

*ESC::ExitApp

*Numpad1::
	MouseGetPos, sx, sy
	MousePosition := "", MousePosition := New HB_Vector(sx,sy)  ; The starting position for your cursor  [ Set a x and y value (x,y)].
	MouseTarget := "", MouseTarget := New HB_Vector(Random(0,A_ScreenWidth),Random(0,A_ScreenHeight))	; 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)
	SoundBeep, 500 ; <-------- Replace with click or w/e you want
	return

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

; 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
	}
}
HTH
isuckatahk
Posts: 36
Joined: 12 Mar 2020, 02:56

Re: Smooth mousemove / random movement to specified x/y cords

10 Apr 2021, 11:58

Hello HellBent, just realized it's you lol, i watched alot of your tutorials on ahk and you got me interested in it's possibilities and i've made a few basic scripts that have helped me immensely with doing things that i couldn't really do without exhausting myself. I have a muscular condition and use a headmouse so mousemove/click is my savior.

As for your above script where do i insert my x/y for where to click?
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Smooth mousemove / random movement to specified x/y cords

12 Apr 2021, 16:23

isuckatahk wrote:
10 Apr 2021, 11:58
Where do i insert my x/y for where to click?
The staring position

Code: Select all

;This is how you set the starting position for your cursor.
;In this example, the starting position is the same as your cursors position when you trigger the move.
MouseGetPos, sx, sy
MousePosition := New HB_Vector(sx,sy)
And the ending position

Code: Select all

;This is how the ending position is set. 
;In this example the target position is a random x/y within the whole screen on your *primary monitor.
MouseTarget := New HB_Vector( Random( 0 , A_ScreenWidth ) , Random( 0 , A_ScreenHeight ) )

;The random function takes two values. The min value to use and the max value to use.
;So an example from your code would be something like this.
MouseTarget := New HB_Vector( Random( 992 ,1023 ) , Random( 520 , 535 ) ) ;Cord 1

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, Google [Bot] and 120 guests