The script below is to toggle rotation of either one of your screens:
#!/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])
How to use
- Copy the script into an empty file, save it as
toggle-rotate.py
In the head section of the script, set:
- the name of the screen you'd like to toggle (find out by running in a terminal the command
xrandr)
the rotate direction, either left or right (between quotes, like in the example).
# --- set the name of the screen and the rotate direction below
screen = "VGA-1"
rotate = "left"
# ---
Test- run it by the command (two times, from a terminal):
python3 /path/to/toggle-rotate.py
If all works fine, add it to a shortcut key. Choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:
python3 /path/to/toggle-rotate.py
to a shortcut of your choice...
That's it.
Explanation
In the output of the command xrandr, the current rotation of your screen (if any) is mentioned directly after the screen's position, for example:
VGA-1 connected 1024x1280+1680+0 left (normal left inverted right x axis y axis) 376mm x 301mm
In the example, we see: 1024x1280+1680+0 left. The script looks into the line, corresponding to the screen, mentioned in the head of the script. if the screen is rotated, the script runs the (xrandr) command:
xrandr --output <screen_name> --rotate normal
if not, it runs (for example):
xrandr --output <screen_name> --rotate left
To rotate the screen counter-clockwise