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
  • 83,767
Tom
  • 675
  • Half of it (1/3) is here: http://askubuntu.com/questions/737139/how-can-i-quickly-toggle-monitor-orientation-with-a-shortcut-key/737155#737155. To get it clear: a combined action to toggle on/of internal keyboard, set the launcher on left (which is the default) and rotate screen? Then you have to mention what state should be combined with what. – Jacob Vlijm Nov 29 '16 at 17:15
  • @Jacob it is very close! I want to put an icon on a launcher, so that if I tap on it the screen rotates and keyboard gets disabled. Then if I tap the icon again, the screen comes back to the ordinary position and keyboard gets enabled again. – Tom Nov 29 '16 at 17:26
  • So basically I need to add disable/enable keyboard function to the python script on the link provided by Jacob. – Tom Nov 29 '16 at 17:28
  • Should be no problem. If no one does before I do, I will post today or tomorrow. – Jacob Vlijm Nov 29 '16 at 17:30
  • Hi Tom, posted my answer. Please mention if all is clear! – Jacob Vlijm Nov 29 '16 at 20:36
  • Wait, you also need to disable touchpad? Can be done, but need to know. – Jacob Vlijm Nov 29 '16 at 20:56
  • @Jacob I want to disable touch pad too – Tom Nov 29 '16 at 23:40

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
  • 83,767
  • Thank you so much! It would be great if it can also disable touchpad and mouse clicks too! – Tom Nov 29 '16 at 23:42
  • Also, I think this will help loads and loads of people who uses ubuntu on their 2 in 1. If you don't mind I would like to link this to few places (perhaps a blog post or something) cheers! – Tom Nov 29 '16 at 23:43
  • I think that the "9" in the last line should be keyboard – Tom Nov 30 '16 at 00:21
  • yeah, disabling touchpad was easy. Now the problem is that keyboard gets disabled except for all the multimedia keys (brightness, airplane mode, sounds, etc... ). – Tom Nov 30 '16 at 01:03