Skip to content

Instantly share code, notes, and snippets.

@lucassilvas1
Last active September 3, 2025 20:46
Show Gist options
  • Select an option

  • Save lucassilvas1/ace05e0834ac86284c3f6398bf2c8e0e to your computer and use it in GitHub Desktop.

Select an option

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
#SingleInstance Force
SetWinDelay 0 ; Remove delay between window and mouse movements
CoordMode "Mouse", "Screen"
F13:: ResizeActiveWindow()
; 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)
UseBackdrop := true ; If true, creates a fullscreen black window to act as a backdrop
MoveWhileMouseDown := false ; If true, windows will be moved even if a mouse button is currently pressed
; Used to quickly resize the active window to a percentage of the screen size
FollowAspectRatio := true ; If true, the window will be resized to keep the aspect ratio
MinWindowWidthPercent := 0.8 ; Minimum width percentage of the screen size a window can be resized to
MinWindowHeightPercent := MinWindowWidthPercent ; Minimum height percentage of the screen size a window can be resized to
MaxWindowWidthPercent := 0.9 ; Maximum width percentage of the screen size a window can be resized to
MaxWindowHeightPercent := MaxWindowWidthPercent ; Maximum height percentage of the screen size a window can be resized to
; ================================
; Used to determine if system is idle
ES_CONTINUOUS := 0x80000000
ES_DISPLAY_REQUIRED := 0x00000002
; ================================
SetTimer Move, Delay
if (UseBackdrop) {
CreateBackdrop()
}
CreateBackdrop() {
backdrop := Gui("-Caption -Resize", "Backdrop")
backdrop.BackColor := "0x000000"
backdrop.OnEvent("Close", (*) => ExitApp)
backdrop.Show("x0 y0 w" A_ScreenWidth " h" A_ScreenHeight)
}
GetExecutionState() {
return DllCall("SetThreadExecutionState", "UInt", 0)
}
IsIdle() {
; If display is off or execution state is not continuous, consider the system idle
return !(GetExecutionState() & (ES_DISPLAY_REQUIRED | ES_CONTINUOUS))
}
; Checks if any mouse button is currently pressed
IsMouseDown() {
return GetKeyState("LButton", "P") || GetKeyState("RButton", "P") || GetKeyState("MButton", "P")
}
ResizeActiveWindow() {
ID := WinGetID("A")
if !(ID) {
return
}
WidthPercent := Random(MinWindowWidthPercent, MaxWindowWidthPercent)
HeightPercent := FollowAspectRatio ? WidthPercent : Random(MinWindowHeightPercent, MaxWindowHeightPercent)
Width := Floor(WidthPercent * A_ScreenWidth)
Height := Floor(HeightPercent * A_ScreenHeight)
XOffset := Floor(Random(0, A_ScreenWidth - Width))
YOffset := Floor(Random(0, A_ScreenHeight - Height))
WinMove(XOffset, YOffset, Width, Height, "ahk_id" ID)
}
Move() {
; If system is idle, don't move windows
if (IsIdle()) {
return
}
if (!MoveWhileMouseDown && IsMouseDown()) {
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 resets idle timers, keeping the system awake
DllCall("SetCursorPos", "Int", MouseX + (Pos.X - X), "Int", MouseY + (Pos.Y - Y))
}
}
}
}
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