如何使用快捷键快速切换显示器方向?

use*_*853 4 xrandr shortcut-keys multiple-monitors external-monitor orientation

在查看以前是否有人问过这个问题时,我发现了Windows 的这个问题

我想在 Linux/Ubuntu 上执行类似的操作(快捷方式或终端别名/命令),以便能够在横向和纵向模式之间快速切换外部显示器,而不必转到“显示”进行设置并确认配置。

Jacob Vlijm 提供了一个可以运行的Python 脚本。如果您有其他想法,我很想知道。

更新:我已经更新了 Jacob 的脚本,使其适用于两个已连接的屏幕

Jac*_*ijm 5

下面的脚本用于切换任一屏幕的旋转:

#!/usr/bin/env python3
import subprocess

# --- set the name of the screen and the rotate direction below
screen = "VGA-1"
rotate = "left"
# ---

matchline = [
    l.split() for l in subprocess.check_output(["xrandr"]).decode("utf-8").splitlines()\
    if l.startswith(screen)
    ][0]
s = matchline[
    matchline.index([s for s in matchline if s.count("+") == 2][0])+1
    ]

rotate = "normal" if s == rotate else rotate
subprocess.call(["xrandr", "--output", screen, "--rotate", rotate])
Run Code Online (Sandbox Code Playgroud)

如何使用

  1. 将脚本复制到一个空文件中,另存为toggle-rotate.py
  2. 在脚本的头部,设置:

    • 您想要切换的屏幕的名称(通过在终端中运行命令来查找xrandr
    • 旋转方向,或者leftright在引号之间,如示例中所示)。

      # --- set the name of the screen and the rotate direction below
      screen = "VGA-1"
      rotate = "left"
      # ---
      
      Run Code Online (Sandbox Code Playgroud)
  3. 测试 - 通过命令运行它(两次,从终端):

    python3 /path/to/toggle-rotate.py
    
    Run Code Online (Sandbox Code Playgroud)
  4. 如果一切正常,请将其添加到快捷键。选择:系统设置>“键盘”>“快捷方式”>“自定义快捷方式”。单击“+”并添加命令:

    python3 /path/to/toggle-rotate.py
    
    Run Code Online (Sandbox Code Playgroud)

    到您选择的快捷方式...

就是这样。

解释

在命令的输出中xrandr,屏幕的当前旋转(如果有)直接在屏幕位置之后提及,例如:

VGA-1 connected 1024x1280+1680+0 left (normal left inverted right x axis y axis) 376mm x 301mm
Run Code Online (Sandbox Code Playgroud)

在示例中,我们看到:1024x1280+1680+0 left。该脚本查看脚本头部提到的与屏幕相对应的行。如果屏幕旋转,脚本将运行 ( xrandr) 命令:

xrandr --output <screen_name> --rotate normal
Run Code Online (Sandbox Code Playgroud)

如果不是,它将运行(例如):

xrandr --output <screen_name> --rotate left
Run Code Online (Sandbox Code Playgroud)

逆时针旋转屏幕