Skip to content

Instantly share code, notes, and snippets.

@ryanmoralesaz
Created July 17, 2025 22:57
Show Gist options
  • Select an option

  • Save ryanmoralesaz/e489d641569fa1626d5d8f1a188a2448 to your computer and use it in GitHub Desktop.

Select an option

Save ryanmoralesaz/e489d641569fa1626d5d8f1a188a2448 to your computer and use it in GitHub Desktop.
Global Cursor Move Helper AHK script
#Requires AutoHotkey v2.0
#SingleInstance Force
#Warn
; Global variable for grid navigation - single position tracker
global gridPosition := 1
; Helper function to move mouse to grid position
MoveToGridPosition(position) {
; Get the monitor where the mouse currently is
CoordMode("Mouse", "Screen")
MouseGetPos(&mouseX, &mouseY)
; Find which monitor contains the mouse
monitorCount := MonitorGetCount()
currentMonitor := 1
loop monitorCount {
MonitorGet(A_Index, &mLeft, &mTop, &mRight, &mBottom)
if (mouseX >= mLeft && mouseX < mRight && mouseY >= mTop && mouseY < mBottom) {
currentMonitor := A_Index
break
}
}
; Get the current monitor's dimensions
MonitorGet(currentMonitor, &Left, &Top, &Right, &Bottom)
monWidth := Right - Left
monHeight := Bottom - Top
; Different padding for horizontal and vertical
paddingH := monWidth * 0.1 ; 10% horizontal padding
paddingVTop := monHeight * 0.05 ; 5% top padding (less padding = closer to edge)
paddingVBottom := monHeight * 0.05 ; 5% bottom padding
effectiveWidth := monWidth - (paddingH * 2)
effectiveHeight := monHeight - (paddingVTop + paddingVBottom)
cellWidth := effectiveWidth / 3
cellHeight := effectiveHeight / 3
; Calculate grid position
gridRow := (position - 1) // 3
gridCol := Mod(position - 1, 3)
; Calculate target coordinates with adjusted vertical spacing
; Calculate target X - special handling for positions 4 and 6
if (position = 4) { ; Middle-left - closer to left edge
targetX := Left + paddingH + (cellWidth / 8) ; Use 1/4 into cell (closer to left)
} else if (position = 6) { ; Middle-right - closer to right edge
targetX := Left + paddingH + (2 * cellWidth) + (3 * cellWidth / 3) ; Use 3/4 into cell (closer to right)
} else { ; All other positions - normal centering
targetX := Left + paddingH + (gridCol * cellWidth) + (cellWidth / 2)
}
; Adjust Y position to push top row up and bottom row down
if (gridRow = 0) { ; Top row - closer to top
targetY := Top + paddingVTop + (cellHeight / 3) ; Use 1/3 into cell instead of center
} else if (gridRow = 2) { ; Bottom row - closer to bottom
targetY := Top + paddingVTop + (2 * cellHeight) + (2 * cellHeight / 3) ; Use 2/3 into cell
} else { ; Middle row - centered
targetY := Top + paddingVTop + gridRow * cellHeight + (cellHeight / 2)
}
; Move mouse
MouseMove(targetX, targetY, 0)
; Tiny movement to make cursor visible
Sleep(10)
MouseMove(targetX + 1, targetY, 0)
MouseMove(targetX, targetY, 0)
; Visual feedback
ToolTip("Position " . position)
SetTimer(() => ToolTip(), -600)
}
; Forward navigation - FIXED: Move first, then update
^!z:: {
global gridPosition
gridPosition := gridPosition + 1
; Update position for next time
if (gridPosition > 9) {
gridPosition := 1
}
MoveToGridPosition(gridPosition)
}
; Backward navigation - FIXED: Update first, then move
^!+z:: {
global gridPosition
; Update position first
gridPosition := gridPosition - 1
if (gridPosition < 1) {
gridPosition := 9
}
MoveToGridPosition(gridPosition)
}
; Reset to position 1
^!x:: {
global gridPosition
gridPosition := 1
MoveToGridPosition(1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment