I'm making this object tracking script using autohotkey. I was able to get it to work in game, where the mouse follows the object. However, the mouse tracking movement is slow, choppy, and has a delayed reaction time to the object movement. I'm trying to achieve as close to a 1:1 tracking effect, if that makes sense. Any help would be appreciated.
Code: Select all
ScaleFactor := 20 ; Set the ScaleFactor to 1
#Include mousedelta.ahk
; Instantiate the MouseDelta class
Mouse := new MouseDelta(Func("MouseMoveCallback"))
; Flag to track whether DPI script is active
DPIActive := false
; Function to move the mouse cursor smoothly to a specific position on the screen
SmoothMoveMouseTo(targetX, targetY, duration, speedFactor) {
; Activate DPI script to increase mouse speed
DPIActive := true
; Get the current mouse position
MouseGetPos, mouseX, mouseY
; Calculate the distance to move
distanceX := targetX - mouseX
distanceY := targetY - mouseY
; Calculate the total distance
totalDistance := Sqrt(distanceX ** 2 + distanceY ** 2)
; Calculate step increments
steps := Max(Abs(distanceX), Abs(distanceY))
stepX := distanceX / steps
stepY := -1 * (distanceY / steps) ; Invert y-axis movement
; Calculate step duration based on total distance and desired duration
stepDuration := duration / totalDistance
; Adjust step duration based on speed factor
stepDuration := stepDuration / speedFactor
; Perform smooth mouse movement
Loop, % steps {
mouseX += stepX
mouseY += stepY
DllCall("mouse_event", UInt, 1, Int, Round(stepX), Int, Round(stepY), UInt, 0, UInt, 0)
,stepDuration
}
; Deactivate DPI script after mouse movement
DPIActive := false
}
; Function to move the mouse cursor smoothly towards the center of the target
SmoothMoveToCenter(targetX, targetY, centerX, centerY, duration, speedFactor) {
; Set higher mouse speed temporarily
SetDefaultMouseSpeed, 20
; Move the mouse cursor smoothly to the center of the target
SmoothMoveMouseTo(targetX, targetY, duration, speedFactor)
; Restore default mouse speed
SetDefaultMouseSpeed, 2 ; Adjust to your default mouse speed value
}
; Mouse move callback function for DPI script
MouseMoveCallback(ThisMouse, x, y) {
; Check if DPI script is active
if (DPIActive) {
; Increase mouse speed here (implement DPI script logic)
; For example, you can adjust mouse speed using SetDefaultMouseSpeed
SetDefaultMouseSpeed, 20
}
}
; Function to read coordinates from the named pipe
ReadCoordinatesFromPipe() {
; Create named pipe handle
pipe := DllCall("CreateFile", "Str", "\\.\pipe\SamplePipe", "UInt", 0xC0000000, "UInt", 3, "Ptr", 0, "UInt", 3, "UInt", 0, "Ptr", 0)
; Check if the pipe was opened successfully
if (pipe = -1) {
MsgBox, Failed to connect to named pipe.
ExitApp
}
; Infinite loop to read coordinates from the pipe
Loop {
VarSetCapacity(data, 8)
; Check if there is data available in the pipe
DllCall("PeekNamedPipe", "Ptr", pipe, "Ptr", 0, "UInt", 0, "Ptr", 0, "PtrP", bytesAvailable, "Ptr", 0)
; Read data from the pipe if available
if (bytesAvailable > 0) {
; Read the data from the pipe
bytesRead := DllCall("ReadFile", "Ptr", pipe, "Ptr", &data, "UInt", 8, "Ptr", 0, "UInt", 0)
; Check if data was read successfully
if (bytesRead > 0) {
; Extract x and y coordinates from the data
x := NumGet(data, 0, "Int")
y := NumGet(data, 4, "Int")
; Set the center coordinates of the screen (replace these with your screen's center coordinates)
centerX := A_ScreenWidth / 2
centerY := A_ScreenHeight / 2
; Move the mouse cursor smoothly towards the center of the target
SmoothMoveToCenter(x, y, centerX, centerY, 5, 2000) ; Adjust duration and speed factor as needed
} else if (bytesRead < 0) {
MsgBox, Error reading from named pipe.
ExitApp
}
}
}
}
; Start reading coordinates from the named pipe
ReadCoordinatesFromPipe()
; Hotkeys for Control
Pause:: Pause
^Esc:: ExitApp
; Gets called when mouse moves or stops
; x and y are DELTA moves (Amount moved since last message), NOT coordinates.
MouseEvent(MouseID, x := 0, y := 0){
global ScaleFactor
if (MouseID){
x *= ScaleFactor, y *= ScaleFactor
DllCall("mouse_event",uint,1,int, x ,int, y,uint,0,int,0)
}
}