Snippets

YellowAfterlife Monitor switching macro (Dec 2016)

Created by Vadim Dyachenko
Const $Mons = 2 ; How many monitors have you got?
Global $MouseX[$Mons], $MouseY[$Mons], $CurMon
Global $MonX[$Mons], $MonY[$Mons], $MonW[$Mons], $MonH[$Mons], $HWnd[$Mons]
; Monitor setup
Func SetupMon($index, $x, $y, $width, $height)
	$MonX[$index] = $x
	$MonY[$index] = $y
	$MonW[$index] = $width
	$MonH[$index] = $height
	$MouseX[$index] = $x + $width / 2
	$MouseY[$index] = $y + $height / 2
	$HWnd[$index] = 0;
EndFunc
; Setup your monitors here:
SetupMon(0, 1920*0, 0, 1920, 1080)
SetupMon(1, 1920*1, 0, 1920, 1080)
;
Func SwitchToMon($index)
	$mx = MouseGetPos(0)
	$my = MouseGetPos(1)
	; Find, on which monitor the mouse currently is:
	$mm = -1
	For $i = 0 To $Mons - 1
		If ($mx >= $MonX[$i]) And ($mx < $MonX[$i] + $MonW[$i]) And ($my >= $MonY[$i]) And ($my < $MonY[$i] + $MonH[$i]) Then
			$mm = $i
		EndIf
	Next
	; Handle
	If ($index == "-") Then
		$index = $mm - 1
		If ($index < 0) Then
			$index = $index + $Mons
		EndIf
	EndIf
	If ($index == "+") Then
		$index = Mod($mm + 1, $Mons)
	EndIf
	; Store the current monitor' mouse coordinates:
	If ($mm >= 0) Then
		$MouseX[$mm] = $mx
		$MouseY[$mm] = $my
		$HWnd[$mm] = WinGetHandle("[ACTIVE]")
	EndIf
	; Switch to the target monitor:
	If ($mm <> $index) Then
		MouseMove($MouseX[$index], $MouseY[$index], 0)
		$hdl = $HWnd[$index]
		If ($hdl <> 0 And WinExists($hdl)) Then
			WinActivate($hdl)
		EndIf
	EndIf
EndFunc
; Bindings:
Func SwitchToMon0()
	SwitchToMon(0)
EndFunc
HotKeySet("^#{LEFT}", "SwitchToMon0")
Func SwitchToMon1()
	SwitchToMon(1)
EndFunc
HotKeySet("^#{RIGHT}", "SwitchToMon1")
; Loop:
While (True)
	Sleep(1000)
WEnd

Comments (0)