Skip to content

Commit

Permalink
Ability to lock monitors
Browse files Browse the repository at this point in the history
  • Loading branch information
lhns committed Aug 24, 2018
1 parent e1ab059 commit 66d2502
Showing 1 changed file with 46 additions and 17 deletions.
63 changes: 46 additions & 17 deletions switchmonitor.au3
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,39 @@ Opt("TrayAutoPause", 0)
Opt("TrayMenuMode", 3)
Opt("TrayOnEventMode", 1)

Main()
HotKeySet("#{PGUP}", "NextMon")
HotKeySet("#{PGDN}", "PrevMon")

Func Main()
HotKeySet("#{PGUP}", "NextMon")
HotKeySet("#{PGDN}", "PrevMon")
TrayCreateEventItem("Next Monitor", "NextMon")
TrayCreateEventItem("Previous Monitor", "PrevMon")
TrayCreateItem("")
$idLockMonitor = TrayCreateEventItem("Lock Monitor", "ToggleLockMonitor")
TrayCreateItem("")
TrayCreateEventItem("Exit", "ExitScript")

TrayCreateEventItem("Next Monitor", "NextMon")
TrayCreateEventItem("Previous Monitor", "PrevMon")
TrayCreateItem("")
TrayCreateEventItem("Exit", "ExitScript")
Global $lockMonitor = False
Global $monitor = GetMonitor(MouseGetPos(0))

Idle()
EndFunc
Loop()

Func Loop()
Local $lastUpdated = 0

Func Idle()
While 1
Sleep(60000)
If $lockMonitor Then
Local $currentMonitor = GetMonitor(MouseGetPos(0))
If $currentMonitor > $monitor Then
MouseMove(@DesktopWidth * ($monitor + 1) - 1, MouseGetPos(1), 0)
$lastUpdated = TimerInit()
ElseIf $currentMonitor < $monitor Then
MouseMove(@DesktopWidth * $monitor, MouseGetPos(1), 0)
$lastUpdated = TimerInit()
EndIf
EndIf

If TimerDiff($lastUpdated) > 1000 Then
Sleep(100)
EndIf
WEnd
EndFunc

Expand All @@ -29,8 +45,13 @@ Func ExitScript()
EndFunc

Func TrayCreateEventItem($text, $function)
TrayCreateItem($text)
Local $id = TrayCreateItem($text)
TrayItemSetOnEvent(-1, $function)
Return $id
EndFunc

Func GetMonitor($mouseX)
Return Floor($mouseX / @DesktopWidth)
EndFunc

Func VirtScreenWidth()
Expand All @@ -46,15 +67,23 @@ Func VirtScreenMaxX()
EndFunc

Func NextMon()
$newX = MouseGetPos(0) + @DesktopWidth
If ($newX <= VirtScreenMaxX()) Then
Local $newX = MouseGetPos(0) + @DesktopWidth
If $newX <= VirtScreenMaxX() Then
MouseMove($newX, MouseGetPos(1),0)
$monitor = GetMonitor(MouseGetPos(0))
EndIf
EndFunc

Func PrevMon()
$newX = MouseGetPos(0) - @DesktopWidth
if ($newX >= VirtScreenMinX()) Then
Local $newX = MouseGetPos(0) - @DesktopWidth
if $newX >= VirtScreenMinX() Then
MouseMove($newX, MouseGetPos(1),0)
$monitor = GetMonitor(MouseGetPos(0))
EndIf
EndFunc

Func ToggleLockMonitor()
$lockMonitor = Not($lockMonitor)
$monitor = GetMonitor(MouseGetPos(0))
TrayItemSetState($idLockMonitor, 1 + Not($lockMonitor) * 3)
EndFunc

0 comments on commit 66d2502

Please sign in to comment.