Skip to content

Instantly share code, notes, and snippets.

@hkdsun
Last active July 18, 2023 05:09
Show Gist options
  • Save hkdsun/37b48f5968ee910cfbbb04de5da87fa2 to your computer and use it in GitHub Desktop.
Save hkdsun/37b48f5968ee910cfbbb04de5da87fa2 to your computer and use it in GitHub Desktop.
switch_displays.ahk
#Include Monitor.ahk
DllCall("Kernel32.dll\LoadLibrary", "Str", "Dxva2.dll", "Ptr")
getMonitorHandles()
{
hMon := DllCall("MonitorFromPoint"
, "int64", 0x00000001
, "uint", 1) ; flag to return primary monitor on failure
PhysMons := 0
GetNumberOfPhysicalMonitorsFromHMONITOR := DllCall("dxva2\GetNumberOfPhysicalMonitorsFromHMONITOR"
, "int", hMon
, "uint*", &PhysMons)
DllCall("dxva2\GetPhysicalMonitorsFromHMONITOR"
, "int", hMon
, "uint", PhysMons
, "ptr", PHYS_MONITORS := Buffer((A_PtrSize + 256) * PhysMons, 0))
handles := Array()
Loop PhysMons {
offset := A_Index - 1
hPhysMon := NumGet(PHYS_MONITORS, offset * (A_PtrSize + 256), "ptr")
handles.Push(hPhysMon)
}
return handles
}
destroyMonitorHandle(handle)
{
DllCall("dxva2\DestroyPhysicalMonitor", "int", handle)
}
setMonitorInputSource(source)
{
handles := getMonitorHandles()
Loop handles.Length {
handle := handles[A_Index]
DllCall("dxva2\SetVCPFeature"
, "int", handle
, "char", 0x60 ;VCP code for Input Source Select
, "uint", source)
destroyMonitorHandle(handle)
}
}
getMonitorInputSource()
{
handles := getMonitorHandles()
Loop handles.Length {
handle := handles[A_Index]
currentValue := 0
maximumValue := 0
DllCall("dxva2\GetVCPFeatureAndVCPFeatureReply"
, "int", handle
, "char", 0x60 ;VCP code for Input Source Select
, "Ptr", 0
, "uint*", &currentValue
, "uint*", &maximumValue)
destroyMonitorHandle(handle)
MsgBox("Current Value: " currentValue "`nMaximum Value: " maximumValue)
}
}
extendDisplays()
{
run("C:\Windows\System32\DisplaySwitch.exe /extend")
}
mirrorDisplays()
{
run("C:\Windows\System32\DisplaySwitch.exe /clone")
}
WindowsExtended()
{
; Use MsgBox(get_monitor_input_source()) to find your input code argument below
setMonitorInputSource(4369)
extendDisplays()
}
MacbookOne()
{
setMonitorInputSource(4379)
mirrorDisplays()
}
MacbookTwo() {
setMonitorInputSource(4367)
mirrorDisplays()
}
WindowsMirrored()
{
setMonitorInputSource(4369)
mirrorDisplays()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment