1

I have 2 in 1 pc with Ubuntu. I was wondering if there is a way to write a command which toggles an internal keyboard on and off, and rotates the screen 90 degrees left (or right), and put it on a launcher on the left. Is there any way to do that?

It would be nice, if I can just tap it to disable keyboard (+ touchpad) and rotate the screen 90 degrees, and tap one more time to go back to the right orientation with functioning keyboard (+touchpad).

Jacob Vlijm
  • 85,475
Tom
  • 725

1 Answers1

1

Script + launcher to rotate the screen and toggle Keyboard, in one step

The script below will both

  • rotate (toggle) your screen (either left or right)
  • disable (toggle) the keyboard you defined to be disabled

    enter image description here

    enter image description here

The script

#!/usr/bin/env python3
import subprocess

# --- set the name of the screen, and the rotate direction and the id of your keyboard below
screen = "DVI-I-1"
rotate = "left"
disable = ["9", "14"]
# ---

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
    ]

newset = ["normal", "1"] if s == rotate else [rotate, "0"]
subprocess.call(["xrandr", "--output", screen, "--rotate", newset[0]])
for item in disable:
    subprocess.call(["xinput", "set-prop", item, "Device Enabled", newset[1]])

How to use

  1. Copy the script into an empty file, save it as rotate.py
  2. Copy the launcher below into an empty file, save it as rotate.desktop

    [Desktop Entry]
    Exec=python3 /path/to/rotate.py
    Icon=preferences-desktop-keyboard
    Name=Rotate & Disable Keyboard
    Type=Application
    

    In the line Exec=, replace the /path/to/rotate.py by the real path to the rotate.py script. Make the .desktop file executable. It will show an icon like:

    enter image description here

  3. Now you have to find out two things:

    a. the name of your screen (the one you want to rotate): Run in a terminhal:

    xrandr
    

    Look for a line with "connected" in it. The first string is the screen name, looking like the example in the script. (could also be VGA-1 or something like that)

    b. The id of your keyboard, the one to be disabled. Run in a terminal the command:

    xinput -list
    

    as described in this answer.

  4. Enter the found items in the head of the script:

    # --- set the name of the screen, and the rotate direction and the id of your keyboard below
    screen = "DVI-I-1"
    rotate = "left"
    disable = ["9"]
    # ---
    

Now you're done, either use the .desktop file directly from your desktop or move it to ~/.local/share/applications and drag it to the launcher from Dash.

If you do the latter, note that after rotating the screen, the icon will be unresponsive for appr. 7 seconds.

Have fun!

Jacob Vlijm
  • 85,475