Skip to content

Instantly share code, notes, and snippets.

@lucassilvas1
Last active June 19, 2025 21:47
Show Gist options
  • Save lucassilvas1/ace05e0834ac86284c3f6398bf2c8e0e to your computer and use it in GitHub Desktop.
Save lucassilvas1/ace05e0834ac86284c3f6398bf2c8e0e to your computer and use it in GitHub Desktop.
OLED Window Mover
/*
Author: https://github.com/lucassilvas1
License: MIT
This script will move all windows on the screen randomly every minute (can be easily tweaked).
It will also move the cursor if it's hovering over the active window.
The script will stop moving windows if the user has been idle for more than 2 minutes so the system can go to sleep.
Remarks:
- Like any AHK script, only works on Windows;
- Unlikely to work with multi-monitor setups, and I don't have one to test;
*/
#Requires AutoHotkey v2.0
InstallMouseHook true ; Required for `A_TimeIdlePhysical` to work
SetWinDelay 0 ; Remove delay between window and mouse movements
CoordMode "Mouse", "Screen"
; Used to randomize movement (px)
OffsetMin := 15
OffsetMax := 40
AspectRatio := A_ScreenWidth / A_ScreenHeight ; Used to correct offsets in the X axis
Delay := 0.5 * 60 * 1000 ; How long to wait between movements (ms)
IdleLimit := 2 * 60 * 1000 ; Used to stop moving windows when the user is idle (ms)
SetTimer Move, Delay
Move() {
; If the user is idle, don't move windows
if (A_TimeIdlePhysical > IdleLimit) {
return
}
ActiveID := ""
try {
ActiveID := WinGetID("A")
}
for ID in WinGetList() {
AHKID := "ahk_id" ID
try {
; Moving maximized windows causes them to be restored, so we skip them
if (WinGetMinMax(AHKID) != 0) {
continue
}
WinGetPos(&X, &Y, &Width, &Height, AHKID)
; Moving fullscreen windows is pointless and can cause problems with certain games
if (Width >= A_ScreenWidth and Height >= A_ScreenHeight) {
continue
}
MouseGetPos(&MouseX, &MouseY, &MouseWindowID)
Pos := GetNewPos(X, Y, Width, Height)
WinMove(Pos.X, Pos.Y, , , AHKID)
; Move the cursor if it was hovering over the active window
if (ID == ActiveID and MouseWindowID == ActiveID) {
MouseMove(MouseX + (Pos.X - X), MouseY + (Pos.Y - Y), 0)
}
}
}
}
GetOffset(Min, Max) {
return Random(Min, Max)
}
; Returns 1 or -1 randomly. Used as a multiplier to randomize the direction of the movement.
GetDirectionMultiplier() {
return Random(0, 1) ? 1 : -1
}
/*
This function is used to get the new position of the window.
It will randomize the offset and make sure the window doesn't go off screen.
*/
GetNewPos(X, Y, Width, Height) {
FixedX := X + GetOffset(OffsetMin, OffsetMax) * GetDirectionMultiplier() * AspectRatio
FixedY := Y + GetOffset(OffsetMin, OffsetMax) * GetDirectionMultiplier()
/*
Offset 1 pixel from the edge of the screen to avoid windows being visible at the edge
of the screen when they are moved. Don't know why this happens, but it does.
*/
if (FixedX + Width > A_ScreenWidth) {
FixedX := A_ScreenWidth - Width - 1
}
else if (FixedX < 0) {
FixedX := 1
}
if (FixedY + Height > A_ScreenHeight) {
FixedY := A_ScreenHeight - Height - 1
}
else if (FixedY < 0) {
FixedY := 1
}
return { X: FixedX, Y: FixedY }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment