Change Monitor Input with AHK

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
darb21
Posts: 1
Joined: 23 Feb 2018, 12:22

Change Monitor Input with AHK

23 Feb 2018, 13:04

Hi everyone... I'm a total novice with AHK. Been using it for years, but I really only mess with it when I have something that I need to get done... I do enough to make it work, then dont touch it for months again.

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)
Elgin
Posts: 124
Joined: 30 Sep 2013, 09:19

Re: Change Monitor Input with AHK

24 Feb 2018, 01:35

Hi there!

One problem I can see is that in:

Code: Select all

setMonitorInputSource(source)
{
  DllCall("dxva2\SetVCPFeature"
    , "int", handle
    , "char", 0x60 ;VCP code for Input Source Select
    , "uint", source)
}
The handle is undefined and the correct working of the function is thus random at best.

Untested, but this should probably be:

Code: Select all

setMonitorInputSource(source)
{
  global hMon
  DllCall("dxva2\SetVCPFeature"
    , "int", hMon
    , "char", 0x60 ;VCP code for Input Source Select
    , "uint", source)
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 217 guests