Some time ago, I either came across this script, or cobbled it together from various threads. The purpose is to use MCCS and VCP (https://en.wikipedia.org/wiki/Monitor_C ... ommand_Set) to send a signal to the monitor to change inputs. The reason I would like to do this is because my monitor has a KVM built into it, so the mouse and keyboard follow the video signal, but there isn't a keyboard shortcut to make the KVM swap to the other input... so I'm trying to make AHK do the keyboard shortcut for me so that I don't have to use the "touch buttons" on the monitor itself... because they're terrible and don't respond very well.
The script works... When I press Shitf+Alt+m on the PC, it does indeed make the monitor switch from DP input to HDMI input (MacBook), and as expected, the keyboard and mouse follow. On the MacBook, I have hacked together a solution with Better Touch Tool and a shell script to accomplish the task of switching back to PC with the same keyboard shortcut. The problem then comes into play when I switch back to the PC. It gets back to the PC input, and everything looks fine, so I go about my work. Then at some point, I want to switch back to the MacBook for whatever reason.... At this point, the hotkey stops working. If I press the hotkey, I can see it executing code in the console, but it has no effect. Once I reload the script, it works again... but it only works for one "cycle" again. Any clue why this happens?
One other thing to note is that when the KVM switches the inputs, I'm pretty sure it essentially REMOVES the input from the machine you're switching away from... As if you were to unplug the keyboard and mouse. This may have some effect with AHK or this script that I'm not aware of.
Any help would be appreciated.
So here is the script:
Code: Select all
;------------------------------------------------------------------------------
; Settings
;------------------------------------------------------------------------------
#NoEnv ; For security
#SingleInstance force
;------------------------------------------------------------------------------
; Monitor Input to HDMI
;------------------------------------------------------------------------------
!+m::
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
; Initialize Monitor handle
hMon := DllCall("MonitorFromPoint"
, "int64", 0 ; point on monitor
, "uint", 1) ; flag to return primary monitor on failure
; Find number of Physical Monitors
DllCall("dxva2\GetNumberOfPhysicalMonitorsFromHMONITOR"
, "int", hMon
, "uint*", nMon)
; Get Physical Monitor from handle
VarSetCapacity(Physical_Monitor, (A_PtrSize ? A_PtrSize : 4) + 128, 0)
DllCall("dxva2\GetPhysicalMonitorsFromHMONITOR"
, "int", hMon ; monitor handle
, "uint", nMon ; monitor array size
, "int", &Physical_Monitor) ; point to array with monitor
hPhysMon := NumGet(Physical_Monitor)
;DllCall("dxva2\GetVCPFeatureAndVCPFeatureReply"
; , "int", handle
; , "char", 0x60 ;VCP code for Input Source Select
; , "Ptr", 0
; , "uint*", currentValue
; , "uint*", maximumValue)
;MsgBox % "Monitor description: " . StrGet(&Physical_Monitor+(A_PtrSize ? A_PtrSize : 4), "utf-16");
; . "`nCurrent Input: " . currentValue
; . "`nMax Input: " . maximumValue
setMonitorInputSource(source)
{
DllCall("dxva2\SetVCPFeature"
, "int", handle
, "char", 0x60 ;VCP code for Input Source Select
, "uint", source)
}
setMonitorInputSource(17)