我想让我的显示器从
Windows控制(简单的东西,如更改输入源),但找不到从
Python发送DDC/C++I命令的方法…
关于可以帮助的图书馆或方法的任何线索?
解决方法
这很容易使用
windows monitor API.我不认为有任何Python绑定在那里,pywin32不包含这些功能.然而,使用
ctypes称之为并不困难.
这是一个示例,将显示器切换到软键,然后重新开启;它应该很容易适应变化的输入源等.唯一复杂的部分是获取物理显示器的手柄:
from ctypes import windll,byref,Structure,WinError,POINTER,WINFUNCTYPE
from ctypes.wintypes import BOOL,HMONITOR,HDC,RECT,LParaM,DWORD,BYTE,WCHAR,HANDLE
_MONITORENUMPROC = WINFUNCTYPE(BOOL,POINTER(RECT),LParaM)
class _PHYSICAL_MONITOR(Structure):
_fields_ = [('handle',HANDLE),('description',WCHAR * 128)]
def _iter_physical_monitors(close_handles=True):
"""Iterates physical monitors.
The handles are closed automatically whenever the iterator is advanced.
This means that the iterator should always be fully exhausted!
If you want to keep handles e.g. because you need to store all of them and
use them later,set `close_handles` to False and close them manually."""
def callback(hmonitor,hdc,lprect,lparam):
monitors.append(HMONITOR(hmonitor))
return True
monitors = []
if not windll.user32.EnumdisplayMonitors(None,None,_MONITORENUMPROC(callback),None):
raise WinError('EnumdisplayMonitors Failed')
for monitor in monitors:
# Get physical monitor count
count = DWORD()
if not windll.dxva2.GetNumberOfPhysicalMonitorsFromHMONITOR(monitor,byref(count)):
raise WinError()
# Get physical monitor handles
physical_array = (_PHYSICAL_MONITOR * count.value)()
if not windll.dxva2.GetPhysicalMonitorsFromHMONITOR(monitor,count.value,physical_array):
raise WinError()
for physical in physical_array:
yield physical.handle
if close_handles:
if not windll.dxva2.DestroyPhysicalMonitor(physical.handle):
raise WinError()
def set_vcp_feature(monitor,code,value):
"""Sends a DDC command to the specified monitor.
See this link for a list of commands:
ftp://ftp.cis.nctu.edu.tw/pub/csie/Software/X11/private/VeSaSpecs/VESA_Document_Center_Monitor_Interface/mccsV3.pdf
"""
if not windll.dxva2.SetVCPFeature(HANDLE(monitor),BYTE(code),DWORD(value)):
raise WinError()
# Switch to SOFT-OFF,wait for the user to press return and then back to ON
for handle in _iter_physical_monitors():
set_vcp_feature(handle,0xd6,0x04)
raw_input()
set_vcp_feature(handle,0x01)