You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过命令检测外接显示器处于镜像还是扩展模式?

显示模式检测方案

判断逻辑

xrandr输出本身已经包含了区分镜像/扩展模式的完整参数,判断规则如下:

  • 主显示器:输出行带有primary标记的设备即为当前主显示器
  • 镜像模式:多块激活的显示器分辨率完全一致,且位置偏移均为+0+0
  • 扩展模式:多块激活的显示器位置偏移不同,不会同时出现多个+0+0的偏移值

手动查看方法

直接执行xrandr --query,筛选带connected且后缀有分辨率参数的行即可:

镜像模式示例输出:

eDP-1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 344mm x 194mm
HDMI-1 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 527mm x 296mm

扩展模式示例输出:

eDP-1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 344mm x 194mm
HDMI-1 connected 1920x1080+1920+0 (normal left inverted right x axis y axis) 527mm x 296mm

自动检测脚本

可以直接用以下shell脚本一键输出结果:

#!/bin/bash
# 筛选所有激活的显示设备
IFS=$'\n'
active_displays=($(xrandr --query | grep " connected " | grep -E "[0-9]+x[0-9]+\+[0-9]+\+[0-9]+"))
unset IFS

# 识别主显示器
primary_display=$(printf "%s\n" "${active_displays[@]}" | grep " primary " | awk '{print $1}')
echo "当前主显示器:${primary_display}"

# 仅单显示器的情况
if [ ${#active_displays[@]} -le 1 ]; then
    echo "当前仅单显示器运行,无镜像/扩展模式"
    exit 0
fi

# 提取所有显示设备的分辨率+偏移参数
disp_params=()
for disp in "${active_displays[@]}"; do
    param=$(echo "$disp" | grep -oE "[0-9]+x[0-9]+\+[0-9]+\+[0-9]+")
    disp_params+=("$param")
done

# 去重后判断参数数量
unique_count=$(printf "%s\n" "${disp_params[@]}" | sort -u | wc -l)
if [ "$unique_count" -eq 1 ]; then
    echo "当前显示模式:镜像模式"
else
    echo "当前显示模式:扩展模式"
fi

内容的提问来源于stack exchange,提问作者priyabrata ray

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 05:15:02