Monitor source/input toggle script problems

Post your working scripts, libraries and tools for AHK v1.1 and older
monotony
Posts: 2
Joined: 14 Aug 2017, 14:01

Monitor source/input toggle script problems

14 Aug 2017, 14:20

i found this script in an archived post and then this supposedly overhauled version via reddit:

Code: Select all

; Finds monitor handle
getMonitorHandle()
{
; Initialize Monitor handle
hMon := DllCall("MonitorFromPoint"
  , "int64", 0 ; point on monitor
  , "uint", 1) ; flag to return primary monitor on failure


; Get Physical Monitor from handle
VarSetCapacity(Physical_Monitor, 8 + 256, 0)

DllCall("dxva2\GetPhysicalMonitorsFromHMONITOR"
  , "int", hMon   ; monitor handle
  , "uint", 1   ; monitor array size
  , "int", &Physical_Monitor)   ; point to array with monitor

return hPhysMon := NumGet(Physical_Monitor)
}

destroyMonitorHandle(handle)
{
  DllCall("dxva2\DestroyPhysicalMonitor", "int", handle)
}

getMonitorInputSource()
{
  handle := getMonitorHandle()
  DllCall("dxva2\GetVCPFeatureAndVCPFeatureReply"
  , "int", handle
  , "char", 0x60 ;VCP code for Input Source Select
  , "Ptr", 0
  , "uint*", currentValue
  , "uint*", maximumValue)
destroyMonitorHandle(handle)
return currentValue
}

;~ : Used to set the monitor source, I do not know what happens if you send it a value higher than the maximum.
;~ setMonitorInputSource(source)
{
  handle := getMonitorHandle()
DllCall("dxva2\SetVCPFeature"
  , "int", handle
  , "char", 0x60 ;VCP code for Input Source Select
  , "uint", source)
destroyMonitorHandle(handle)
}  


; Msgbox with current monitor input source
!r::
msgbox % getMonitorInputSource()
return

Code: Select all

; Finds monitor handle
getMonitorHandle()
{
  ; Initialize Monitor handle
  hMon := DllCall("MonitorFromPoint"
    , "int64", 0 ; point on monitor
    , "uint", 1) ; flag to return primary monitor on failure


  ; Get Physical Monitor from handle
  VarSetCapacity(Physical_Monitor, 8 + 256, 0)

  DllCall("dxva2\GetPhysicalMonitorsFromHMONITOR"
    , "int", hMon   ; monitor handle
    , "uint", 1   ; monitor array size
    , "int", &Physical_Monitor)   ; point to array with monitor

  return hPhysMon := NumGet(Physical_Monitor)
}

destroyMonitorHandle(handle)
{
  DllCall("dxva2\DestroyPhysicalMonitor", "int", handle)
}

; Used to change the monitor source
; DVI = 3
; HDMI = 4
; YPbPr = 12
setMonitorInputSource(source)
{
  handle := getMonitorHandle()
  DllCall("dxva2\SetVCPFeature"
    , "int", handle
    , "char", 0x60 ;VCP code for Input Source Select
    , "uint", source)
  destroyMonitorHandle(handle)
}

; Gets Monitor source
getMonitorInputSource()
{
  handle := getMonitorHandle()
  DllCall("dxva2\GetVCPFeatureAndVCPFeatureReply"
    , "int", handle
    , "char", 0x60 ;VCP code for Input Source Select
    , "Ptr", 0
    , "uint*", currentValue
    , "uint*", maximumValue)
  destroyMonitorHandle(handle)
  return currentValue
}

;~ #IfWinActive
Pause::
if(getMonitorInputSource() != 3)
  setMonitorInputSource(3)
else
  setMonitorInputSource(17)
return
problem is, i can't get them to run. i think the problem is, i'm using displayport and want to switch between that input and hdmi (xbox 360). the switch to hdmi works, but it won't toggle back to DP. the first script is supposed to give me a message with the correct monitor values (3 for hdmi, 4 for displayport). i tried several things and i am out of options. any ideas?

(before someone mentions it - i'm aware of "mcontrol", but the tool is shareware, not supported anymore, can't be bought and refuses to work after 21 days.)
monotony
Posts: 2
Joined: 14 Aug 2017, 14:01

Re: Monitor source/input toggle script problems

16 Aug 2017, 06:39

it took me more hours than i'd like to admit, but i found the solution on my own.

this is the function, that works as intended:

Code: Select all

setMonitorInputSource(source)
{
  handle := getMonitorHandle()
  DllCall("dxva2\SetVCPFeature"
  , "int", handle
  , "char", 0x60 ;VCP code for Input Source Select
  , "int", source)
destroyMonitorHandle(handle)
}
for some reason, 4 as a value for display port wasn't interpreted as the correct address. what works, is using hexadecimal inputs, found in this python script and in an archived forum post regarding display port and mcontrol. the correct inputs are 0x11 for hdmi, and 0x0F for display port (might differ depending on what mccs version your monitor supports). so i changed the variable from uint* to int and et voilà: it worked.

so for everyone who's looking into the same issue, this is the complete script i'm using, switching between HDMI and Display Port by the press of Pause:

Code: Select all

; Finds monitor handle
getMonitorHandle()
{
  ; Initialize Monitor handle
  hMon := DllCall("MonitorFromPoint"
    , "int64", 0 ; point on monitor
    , "uint", 1) ; flag to return primary monitor on failure


  ; Get Physical Monitor from handle
  VarSetCapacity(Physical_Monitor, 8 + 256, 0)

  DllCall("dxva2\GetPhysicalMonitorsFromHMONITOR"
    , "int", hMon   ; monitor handle
    , "uint", 1   ; monitor array size
    , "int", &Physical_Monitor)   ; point to array with monitor

  return hPhysMon := NumGet(Physical_Monitor)
}

destroyMonitorHandle(handle)
{
  DllCall("dxva2\DestroyPhysicalMonitor", "int", handle)
}

; Used to change the monitor source
; DVI = 0x03
; HDMI = 0x11
; DP = 0x0F
setMonitorInputSource(source)
{
  handle := getMonitorHandle()
  DllCall("dxva2\SetVCPFeature"
  , "int", handle
  , "char", 0x60 ;VCP code for Input Source Select
  , "int", source)
destroyMonitorHandle(handle)
}

; Gets Monitor source
getMonitorInputSource()
{
  handle := getMonitorHandle()
  DllCall("dxva2\GetVCPFeatureAndVCPFeatureReply"
    , "int", handle
    , "char", 0x60 ;VCP code for Input Source Select
    , "Ptr", 0
    , "uint*", currentValue
    , "uint*", maximumValue)
  destroyMonitorHandle(handle)
  return currentValue
}

;~ #IfWinActive
Pause::
if(getMonitorInputSource() != 3) ;if not HDMI
  setMonitorInputSource(0x11) ;switch to HDMI
else
  setMonitorInputSource(0x0F) ;or Display Port
return

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 120 guests