Center Active Window Using AutoHotkey
A simple solution for centering windows across multiple monitors.
Managing windows across multiple displays can be cumbersome. While Windows offers basic snapping functionality, precisely centering windows requires manual repositioning. This post presents a concise AutoHotkey script that automates window centering with a simple keyboard shortcut.
The Script
Features:
- Hotkey:
Win + C
for instant centering - Automatically detects which monitor contains the active window
- Respects taskbar positioning and reserved screen space
- Compatible with Windows 10/11 and AutoHotkey v2
- Works with multiple monitors, including ultrawide displays
; Center active window
#c:: {
WinGetPos(&X, &Y, &W, &H, "A")
CoordMode("Mouse", "Screen")
MouseGetPos(&mouseX, &mouseY)
monitorNumber := MonitorGetPrimary()
; Find which monitor the window is actually on
Loop MonitorGetCount() {
MonitorGetWorkArea(A_Index, &mLeft, &mTop, &mRight, &mBottom)
if (X >= mLeft && X < mRight && Y >= mTop && Y < mBottom) {
monitorNumber := A_Index
break
}
}
; Calculate perfect center for THAT monitor
MonitorGetWorkArea(monitorNumber, &monitorLeft, &monitorTop, &monitorRight, &monitorBottom)
newX := monitorLeft + (monitorRight - monitorLeft - W) / 2
newY := monitorTop + (monitorBottom - monitorTop - H) / 2
WinMove newX, newY,,, "A"
}
Implementation Guide
- Install AutoHotkey v2+ (Download here).
- Save the script with a
.ahk
extension, such asCenterWindow.ahk
. - Execute the script by right-clicking and selecting “Run Script”. For persistent availability, add it to startup.
Customization Options
- Alternative hotkeys: Modify
#c
to use different key combinations:^!c
forCtrl + Alt + C
#!Down
forWin + Alt + Down Arrow
Reference the AutoHotkey Hotkey Documentation for more options.
- Add animation: Implement smooth transitions if desired.
Known Limitations
- Administrative windows (Task Manager, elevated Command Prompt) require running the script with administrative privileges.
- Fullscreen applications may ignore window positioning commands.
- Ultrawide monitor users can adjust horizontal positioning by modifying the centering calculation:
newX := monitorLeft + (monitorRight - monitorLeft - W) / 3 ; Position 1/3 from left
How It Works
The script functions through three key mechanisms:
1. Position Detection
CoordMode("Mouse", "Screen")
MouseGetPos(&mouseX, &mouseY)
This establishes screen-level coordinates to handle multi-monitor setups properly.
2. Monitor Identification
Loop MonitorGetCount() {
MonitorGetWorkArea(A_Index, &mLeft, &mTop, &mRight, &mBottom)
if (X >= mLeft && X < mRight && Y >= mTop && Y < mBottom) {
monitorNumber := A_Index
break
}
}
This loop determines which monitor contains the active window by comparing window coordinates against each display’s boundaries.
3. Centering Calculation
newX := monitorLeft + (monitorRight - monitorLeft - W) / 2
newY := monitorTop + (monitorBottom - monitorTop - H) / 2
These formulas calculate the optimal window position, accounting for usable screen area (excluding taskbars and docks) and window dimensions.
Conclusion
This lightweight AutoHotkey solution provides efficient window management across multiple displays. With a single keyboard shortcut, you can instantly center any window on its current monitor, eliminating manual repositioning.