Skip to content

Instantly share code, notes, and snippets.

@dashw00d
Created April 15, 2025 15:03
Show Gist options
  • Save dashw00d/d49e598860ae5fc287be501dd7bd5f0d to your computer and use it in GitHub Desktop.
Save dashw00d/d49e598860ae5fc287be501dd7bd5f0d to your computer and use it in GitHub Desktop.
WASD to Mouse Movement Auto Hotkey Script
#SingleInstance Force
SetWorkingDir A_ScriptDir
SendMode "Input"
; Parameters
global fixedDistance := 150 ; Fixed distance from center
global verticalOffset := -40 ; Pixels to offset vertically from center when returning (-ve is up)
global targetMonitorIndex := 1 ; Set the target monitor index (1, 2, 3, etc.)
; State variables
global MouseState := 0 ; 0 = Up, 1 = Down
global CenterX := 0 ; Center X coordinate of target monitor
global CenterY := 0 ; Center Y coordinate of target monitor
; Initial setup: Get target monitor center
CenterMouseOnTargetMonitor()
; Function to find target monitor and update center coordinates
CenterMouseOnTargetMonitor() {
global CenterX, CenterY, targetMonitorIndex
MonitorGet(targetMonitorIndex, &monLeft, &monTop, &monRight, &monBottom)
CenterX := monLeft + (monRight - monLeft) // 2
CenterY := monTop + (monBottom - monTop) // 2
}
; Function to update mouse position based on WASD state
UpdateMousePosition(*) { ; Use wildcard (*) if needed, but might not be necessary if called directly
global MouseState, CenterX, CenterY, fixedDistance, verticalOffset
isW := GetKeyState("w", "P")
isA := GetKeyState("a", "P")
isS := GetKeyState("s", "P")
isD := GetKeyState("d", "P")
dx := 0
dy := 0
numKeysPressed := isW + isA + isS + isD
if (numKeysPressed > 0) {
; Calculate direction vector components
if (isW)
dy -= 1
if (isS)
dy += 1
if (isA)
dx -= 1
if (isD)
dx += 1
; Normalize for diagonal movement to maintain fixed distance
if (Abs(dx) == 1 && Abs(dy) == 1) {
; Approximation: 1 / sqrt(2) is roughly 0.707
dx := Round(dx * fixedDistance * 0.707)
dy := Round(dy * fixedDistance * 0.707)
} else {
; Pure horizontal or vertical
dx *= fixedDistance
dy *= fixedDistance
}
TargetX := CenterX + dx
TargetY := (CenterY + verticalOffset) + dy
if (!MouseState) {
Click "Down", "Left" ; Hold LEFT mouse button
MouseState := 1
}
MouseMove TargetX, TargetY, 0 ; Move to calculated fixed position
} else {
; No WASD keys are pressed
if (MouseState) {
Click "Up", "Left" ; Release LEFT mouse button
MouseMove CenterX, CenterY + verticalOffset, 0 ; Move mouse to offset center
MouseState := 0
}
}
}
; Hotkeys - trigger update on press and release
*w::UpdateMousePosition()
*a::UpdateMousePosition()
*s::UpdateMousePosition()
*d::UpdateMousePosition()
*w Up::UpdateMousePosition()
*a Up::UpdateMousePosition()
*s Up::UpdateMousePosition()
*d Up::UpdateMousePosition()
; Exit script with Esc key
Esc::ExitApp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment