怎么用脚本控制显示器输入

wen 实用脚本 2

本文目录导读:

怎么用脚本控制显示器输入

  1. Windows + PowerShell/CMD
  2. Linux + DDC/CI
  3. 使用 ddcutil(跨平台)
  4. Python 脚本方案
  5. C#/.NET 脚本
  6. 显示器特定工具
  7. 注意事项
  8. 简单自动化脚本示例

我来介绍几种控制显示器输入源(切换HDMI/DP/VGA等)的方法:

Windows + PowerShell/CMD

使用 NirSoft 的 ControlMyMonitor

# 下载 ControlMyMonitor 工具
# 查看显示器支持的命令
.\ControlMyMonitor.exe /ShowCommands
# 切换输入源(需要知道具体的命令代码)
.\ControlMyMonitor.exe /SetValue \\.\DISPLAY1 60 15
# 60=输入源命令代码, 15=HDMI1

使用软DDC/CI控制

# 安装 monitorian (UWP应用)
# 或使用 ddcci-tools

Linux + DDC/CI

# 安装 ddccontrol
sudo apt install ddccontrol
# 或
sudo pacman -S ddccontrol
# 查看显示器
sudo ddccontrol -p
# 切换输入源
ddccontrol -r 0x60 -w 15 dev:/dev/i2c-0
# 0x60 是输入源控制寄存器
# 15 = HDMI1, 16 = HDMI2, 17 = DP1, 18 = DP2

使用 ddcutil(跨平台)

# 安装
sudo apt install ddcutil
# 查看显示器支持的功能
ddcutil capabilities
# 查看输入源选项
ddcutil getvcp 60
# 设置输入源
ddcutil setvcp 60 0x0F  # HDMI1
ddcutil setvcp 60 0x10  # HDMI2
ddcutil setvcp 60 0x11  # DP1
ddcutil setvcp 60 0x12  # DP2

Python 脚本方案

# 安装 py-ddcutil
pip install python-ddcutil
import ddcutil
# 获取显示器列表
monitors = ddcutil.MonitorManager()
for monitor in monitors.list():
    print(monitor)
# 切换到指定输入
def switch_input(monitor_name, input_source):
    with ddcutil.Monitor(monitor_name) as m:
        m.set_vcp(0x60, input_source)
        # 0x0F=HDMI1, 0x10=HDMI2, 0x11=DP1, 0x12=DP2
# 使用
switch_input("Dell U2720Q", 0x11)  # 切换到DP1

C#/.NET 脚本

using System.Runtime.InteropServices;
class MonitorControl
{
    [DllImport("dxva2.dll", EntryPoint = "SetVCPFeature")]
    static extern bool SetVCPFeature(IntPtr hMonitor, byte bVCPCode, uint dwValue);
    public static void SwitchInput(string monitor, byte code, uint value)
    {
        // 获取显示器句柄并设置输入
        // code=0x60, value=输入源编号
    }
}

显示器特定工具

许多品牌有官方CLI工具:

  • LG: LG Screen Control
  • Dell: Dell Display Manager
  • BenQ: Display Pilot

注意事项

  1. 显示器必须支持 DDC/CI

    检查显示器菜单中的 DDC/CI 或 DPMS 选项

  2. 输入源代码参考

    1  = VGA-1
    2  = VGA-2
    3  = DVI-D
    4  = DVI-A
    5  = DVI-I
    15 = HDMI-1
    16 = HDMI-2
    17 = DisplayPort-1
    18 = DisplayPort-2
  3. 权限问题

    • Linux下可能需要 sudo
    • Windows下需要管理员权限

简单自动化脚本示例

#!/bin/bash
# switch_to_hdmi.sh
# 检测系统
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
    # Linux
    ddcutil setvcp 60 15  # 切换到HDMI1
elif [[ "$OSTYPE" == "darwin"* ]]; then
    # macOS
    osascript -e 'tell application "System Events" to tell process "ControlCenter" to perform action "click" of menu item "HDMI"'
elif [[ "$OSTYPE" == "msys" ]]; then
    # Windows
    ControlMyMonitor.exe /SetValue \\.\DISPLAY1 60 15
fi

这些方法中最通用的是 DDC/CI 协议,大多数现代显示器都支持,如果需要更具体的帮助,请告诉我你的操作系统和显示器型号。

抱歉,评论功能暂时关闭!