Skip to content

Instantly share code, notes, and snippets.

@sirkkalap
Created December 10, 2023 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sirkkalap/2799c3bbebb50c256f0947585d4729c3 to your computer and use it in GitHub Desktop.
Save sirkkalap/2799c3bbebb50c256f0947585d4729c3 to your computer and use it in GitHub Desktop.
AutoHotkey script to type WASD keys using relative mouse movements when RShift is pressed.
#Persistent
SetTimer, CheckMouse, 10
return
; Initialize state variables
WDown := false
ADown := false
SDown := false
DDown := false
CheckMouse:
; Save the previous mouse x- and y-coordinates
PrevMouseX := MouseX
PrevMouseY := MouseY
MouseGetPos, mouseX, mouseY
; Set the sensitivity for movement (adjust as needed)
Sensitivity := 5
; Check if the XButton5 (mouse button 5) is pressed
if (GetKeyState("RShift", "P"))
{
; Calculate delta x and delta y
DeltaX := mouseX - PrevMouseX
DeltaY := mouseY - PrevMouseY
; Simulate WASD key presses based on delta x and delta y
if (DeltaX > Sensitivity)
{
if (!DDown)
{
SendInput, {D down}
DDown := true
}
}
else if (DDown)
{
SendInput, {D up}
DDown := false
}
if (DeltaX < -Sensitivity)
{
if (!ADown)
{
SendInput, {A down}
ADown := true
}
}
else if (ADown)
{
SendInput, {A up}
ADown := false
}
if (DeltaY > Sensitivity)
{
if (!SDown)
{
SendInput, {S down}
SDown := true
}
}
else if (SDown)
{
SendInput, {S up}
SDown := false
}
if (DeltaY < -Sensitivity)
{
if (!WDown)
{
SendInput, {W down}
WDown := true
}
}
else if (WDown)
{
SendInput, {W up}
WDown := false
}
}
else
{
; If XButton5 is not pressed, ensure that WASD key presses are released only if any of them was pressed
if (WDown)
SendInput, {W up}
if (ADown)
SendInput, {A up}
if (SDown)
SendInput, {S up}
if (DDown)
SendInput, {D up}
; Reset state variables
WDown := false
ADown := false
SDown := false
DDown := false
}
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment