Change monitor source from DP to HDMI Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Fulminare
Posts: 369
Joined: 26 Jun 2021, 20:15

Change monitor source from DP to HDMI

21 Sep 2021, 06:06

This is a sort of code that goes way above my limited ahk capabilities.
But it somewhat appeared to be working which got my hopes up.

My Monitor is connected to the CUP via a DP cable
My Laptop is connected to the Monitor via a HDMI cable.

There are some scripts online that I believe shift between 2/3 monitor inputs.
I think this is one of them

Code: Select all

; Finds monitor handle based on MousePosition
getMonitorHandle()
{
  MouseGetPos, xpos, ypos
  point := ( ( xpos ) & 0xFFFFFFFF ) | ( ( ypos ) << 32 )
  ; Initialize Monitor handle
  hMon := DllCall("MonitorFromPoint"
    , "int64", point ; 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
}

; Switching sources~
#x::
if(getMonitorInputSource() > 3)
	setMonitorInputSource(3)
else
	setMonitorInputSource(17)
return
On pressing #X the screen goes blank for a couple of seconds and turns on.


as it mentions :

; Used to change the monitor source
; DVI = 3
; HDMI = 4
; YPbPr = 12


So is there a number for DP as well ?

First of all is this the correct script for changing the input source ?

Any help would mean the world to me. I am really finding it difficult to manually change the input source.
Posting this here because for a person who knows coding, understanding the script becomes dead easy.

sincerely requesting for any guidance.

Regards.
Fulminare
Posts: 369
Joined: 26 Jun 2021, 20:15

Re: Change monitor source from DP to HDMI  Topic is solved

23 Sep 2021, 06:56

Update. I found this code by a reddit user CasperHarkin - (https://www.reddit.com/r/AutoHotkey/comments/bmewoh/switching_main_monitors_source_input_help/)
I am posting it here anyway. It's doesn't require any modifications.

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
}


^6::
setMonitorInputSource(0x11) 
Return
^7::
setMonitorInputSource(0x0F)  
Return
Pressing ctrl 6 will shift to HDMI
pressing ctrl 7 will shift back to DP

In my case I have set the laptop to "Second screen only" setting. So I am able to switch between my desktop and laptop.
it's working like a charm.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Giresharu and 263 guests