Move Windows with Hotkeys

This is a more complicated example of what can be done using AutoHotKey. I use this script every day at work and pretty much consider it indispensable when using multiple monitors.

Simply, this script lets you use the keyboard to move windows between monitors. Control-Alt-+ moves the window to the same position on the next monitor to the right, while Control-Alt-— moves the window to the next monitor to the left. Control-Alt-Up can be used to maximize a window across the first two monitors, which comes in really handy when trying to work with multiple Excel workbooks since you can't make it open two instances any more… Overall, I think the code is pretty easy to follow.

SysGet,MaxMonitor,MonitorCount

Menu,Tray,Standard
Menu,Tray,Add
Menu,Tray,Add,Increment Window's Monitor Number,IncrementWindowMonitor
Menu,Tray,Add,Decrement Window's Monitor Number,DecrementWindowMonitor
Menu,Tray,Add,Maximize Window Across First Two Screens,MultiMaximizeWindow
return

^!NumpadAdd::
IncrementWindowMonitor:
Gosub,FindCurrentMonitor

If (CurrMonitor = 0)
return

NewMonitor := CurrMonitor + 1

If (NewMonitor <= MaxMonitor)
Gosub,MoveCurrentWindow
return

^!NumpadSub::
DecrementWindowMonitor:
Gosub,FindCurrentMonitor

If (CurrMonitor = 0)
return

NewMonitor := CurrMonitor - 1

If (NewMonitor >= 1)
Gosub,MoveCurrentWindow
return

^!Up::
MultiMaximizeWindow:
WinGet,WindowState,MinMax,A
SysGet,LeftMonitor,MonitorWorkArea,1
SysGet,RightMonitor,Monitor,2

If (WindowState = 1)
WinRestore,A

WinMove,A,,0,0,RightMonitorRight,LeftMonitorBottom
return

MoveCurrentWindow:
WinGet,WindowState,MinMax,A
SysGet,NewMonitor,Monitor,%NewMonitor%

NewWinX := WinX - MonitorLeft + NewMonitorLeft
NewWinY := WinY - MonitorTop  + NewMonitorTop

If (WindowState = 1)
WinRestore,A

WinMove,A,,%NewWinX%,%NewWinY%

If (WindowState = 1)
WinMaximize,A
return

FindCurrentMonitor:
WinGetPos,WinX,WinY,WinWidth,WinHeight,A
MidpointX := WinX + (WinWidth / 2)
MidpointY := WinY + (WinWidth / 2)

CurrMonitor := 1
Loop %MaxMonitor%
{
SysGet,Monitor,Monitor,%CurrMonitor%
If (MidpointX >= MonitorLeft AND MidpointX <= MonitorRight AND MidpointY >= MonitorTop AND MidpointY <= MonitorBottom)
break
CurrMonitor := CurrMonitor + 1
}

if (CurrMonitor > MaxMonitor)
{
MsgBox What just happened?? This window isn't on an active monitor??
CurrMonitor := 0
return
}
return

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.