In this article, we will explore a script written in AutoHotkey v2.0 that helps automate the process of moving windows between multiple monitors. The script provides hotkey functionality to quickly move an active window between the primary and secondary monitors, as well as a tray menu option for quick access.
Key features
Hotkey support: Move windows between screens with specific keyboard shortcuts.
Why I did it
Managing multiple monitors can be cumbersome without the right tools. For users who work with multiple screens regularly, manually moving windows between them can be tedious. This script offers several benefits:
The script allows you to quickly move windows between monitors using hotkeys (Alt + M
for primary, Alt + N
for secondary), saving time during daily tasks.
With this Script, I no longer need to drag windows manually to the desired monitor. A simple key press or tray menu selection can accomplish the task in seconds.
AutoHotkey is a lightweight, easy-to-use scripting language that runs on Windows systems, making this solution accessible to most users without complex setup.
How to do it
Here’s a breakdown of the script and how it works:
- Environment Declare:
#Requires AutoHotkey v2.0+
It shows we should use AutoHotkey version newer than 2.0.
- Hotkey Setup:
The script uses Alt + M
and Alt + N
as hotkeys to move the currently active window to the primary or secondary monitor, respectively.
Alt & M::MoveWindowToPrimary() ; Alt + M to move window to primary screen
Alt & N::MoveWindowToSecondary() ; Alt + N to move window to secondary screen
This provides instant control over the window’s position with just a key press.
- Monitor Detection:
The GetMonitorInfo() function detects the number of connected monitors and their positions. It uses the system metric value 80 to get the monitor count and then loops through each monitor to gather its work area (left, top, right, bottom).
GetMonitorInfo() {
monitorCount := SysGet(80) ; 80 is the system metric value for MonitorCount
monitors := []
Loop monitorCount {
workArea := []
MonitorGetWorkArea(A_Index, &left, &top, &right, &bottom)
monitor := {}
monitor.Left := left
monitor.Top := top
monitor.Right := right
monitor.Bottom := bottom
monitors.Push(monitor)
}
return monitors
}
This function is crucial because it dynamically identifies the connected monitors and adjusts the window’s position accordingly.
- Move Window Functions:
The MoveWindowToPrimary() and MoveWindowToSecondary() functions are responsible for moving the active window to the primary or secondary screen.
; Move window to primary screen
MoveWindowToPrimary(*) {
monitors := GetMonitorInfo()
if (monitors.Length < 2) {
MsgBox("Only one monitor detected. Cannot move window.")
return
}
; Get handle of active window
activeWin := WinExist("A")
if !activeWin {
MsgBox("No active window found.")
return
}
; Get primary screen position
mainMonitor := monitors[1]
x := mainMonitor.Left
y := mainMonitor.Top
; Move window to primary screen
WinMove(x, y,,, activeWin)
}
; Move window to secondary screen
MoveWindowToSecondary(*) {
monitors := GetMonitorInfo()
if (monitors.Length < 2) {
MsgBox("Only one monitor detected. Cannot move window.")
return
}
; Get handle of active window
activeWin := WinExist("A")
if !activeWin {
MsgBox("No active window found.")
return
}
; Get secondary screen position
secondaryMonitor := monitors[2]
x := secondaryMonitor.Left
y := secondaryMonitor.Top
; Move window to secondary screen
WinMove(x, y,,, activeWin)
}
Both functions follow a similar pattern:
- Detect monitors: The script checks how many monitors are connected.
- Find active window: The function checks for the currently focused window.
- Move the window: The script moves the window based on the monitor's screen coordinates (left, top).
Conclusion
The complete code can be seen here.
HAHAHA44 / MoveWindowBetweenScreens
Move your active windows application between the main Screen and the secondary screen
Run it
- install autoHotKey 2.0
- run the script with autoHotKey
-
Alt
+M
to move the current application to the main screen -
Alt
+N
to move the current application to the secondary screen
If you have multiple screens, please extend the script.
This AutoHotkey script provides an efficient way to manage windows across multiple monitors. With just a few simple hotkeys, you can easily move windows between your primary and secondary monitors. This solution is particularly useful for anyone working in a multi-monitor setup, allowing for better workflow and less manual adjustment of window positions.
Top comments (0)