Skip to content

Instantly share code, notes, and snippets.

@JoeGlines
Last active April 26, 2021 15:32
Show Gist options
  • Save JoeGlines/0f0dee29f84cbaa8a8d3d3b91b02bcde to your computer and use it in GitHub Desktop.
Save JoeGlines/0f0dee29f84cbaa8a8d3d3b91b02bcde to your computer and use it in GitHub Desktop.
;*******************************************************
; Want a clear path for learning AutoHotkey; Take a look at our AutoHotkey Udemy courses. They're structured in a way to make learning AHK EASY
; Right now you can get a coupon code here: https://the-Automator.com/Learn
;*******************************************************
; Original post from rodfell Oct 2007 https://autohotkey.com/board/topic/41463-simple-dual-monitor-window-switch/
;***********adjust next two lines to the monitors you wish to toggle between*******************
SysGet, Mon1, Monitor, 1 ; Grab monitor number 1 create a Mon1 var
sysGet, Mon2, Monitor, 2 ; Grab monitor number 2 create a Mon2 var
;make sure you place the two above monitor lines at top of script
;************************Toggle between monitors*******************************.
#SingleInstance, Force
#NoEnv
#NoTrayIcon ;comment out if you want to have icon present
!RButton:: ;Alt+right mouse click= Throw window clicked on to other window
mousegetpos,,,windowundermouse
wingetpos,x1,y1,w1,h1,ahk_id %windowundermouse%
winget,winstate,minmax,ahk_id %windowundermouse%
m1:=(x1+w1/2>mon1left) and (x1+w1/2<mon1right) and (y1+h1/2>mon1top) and (y1+h1/2<mon1bottom) ? 1:2 ;works out if centre of window is on monitor 1 (m1=1) or monitor 2 (m1=2)
m2:=m1=1 ? 2:1 ;m2 is the monitor the window will be moved to
ratiox:=abs(mon%m1%right-mon%m1%left)-w1<5 ? 0:abs((x1-mon%m1%left)/(abs(mon%m1%right-mon%m1%left)-w1)) ;where the window fits on x axis
ratioy:=abs(mon%m1%bottom-mon%m1%top)-h1<5 ? 0:abs((y1-mon%m1%top)/(abs(mon%m1%bottom-mon%m1%top)-h1)) ;where the window fits on y axis
x2:=mon%m2%left+ratiox*(abs(mon%m2%right-mon%m2%left)-w1) ;where the window will fit on x axis in normal situation
y2:=mon%m2%top+ratioy*(abs(mon%m2%bottom-mon%m2%top)-h1)
w2:=w1 , h2:=h1 ;width and height will stay the same when moving unless reason not to lower in script
;if x axis takes up whole axis OR won't fit on new screen
if abs(mon%m1%right-mon%m1%left)-w1<5 or abs(mon%m2%right-mon%m2%left-w1)<5
x2:=mon%m2%left , w2:=abs(mon%m2%right-mon%m2%left)
if abs(mon%m1%bottom-mon%m1%top)-h1<5 or abs(mon%m2%bottom-mon%m2%top)-h1<5
y2:=mon%m2%top , h2:=abs(mon%m2%bottom-mon%m2%top)
if winstate { ;move maximized window
winrestore,ahk_id %windowundermouse%
winmove,ahk_id %windowundermouse%,,mon%m2%left,mon%m2%top
winmaximize,ahk_id %windowundermouse%
}
else {
x1:=(x1<mon%m1%left)?(mon%m2%left)
x2:=(x1+w1>mon%m1%right)?(mon%m2%right-w2)
y2:=(y1<mon%m1%top)?(mon%m2%top)
y2:=(y1+h1>mon%m1%bottom)?(mon%m2%bottom-h2)
winmove,ahk_id %windowundermouse%,,x2,y2,w2,h2 ;move non-maximized window
}
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment