1

I changed my laptop from a Lenovo U310 to a Dell Inspiron 7537. The Lenovo had a special key for turning off and on the screen, but the Dell doesn't have it. I want to know if there is a command for turning off and on the screen by using the same keyboard shortcut (something like CTRL+A, or similar).

jmrf
  • 93
  • See this answer: http://askubuntu.com/a/253821/72216 To add it to a shortcut key: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command: xset dpms force off. To turn it on again: touch any key. – Jacob Vlijm Dec 09 '14 at 10:33
  • Almost. But I want the same shortcut to turn the screen off and on; besides I don't want that pressing another key or touching the mouse turns on the screen once is off, i.e., the screen only turns on by using the same shortcut that turned it off. Also, using 'xset dpms force off' turns the screen off for just two seconds; then it goes on again without touching anything. – jmrf Dec 09 '14 at 11:36
  • I retracted my close vote, your situation is indeed a bit different. Added my answer. – Jacob Vlijm Dec 09 '14 at 12:42

1 Answers1

4

There are two circumstances that make your situation a bit different from the supposed duplicate(s):

  1. Some process seems to wake your screen up, if your screen wakes up after the xset dpms force off command, it must be. My screen doesn't for example.
  2. If you don't want your screen to wake up, just with any key press, the xset dpms force off command is not doing what you want.

A workaround is the script below. What it does:

  • It looks up your screen's name and the current brightness
  • if the brightness is not equal to zero, it blacks the screen, else it sets brightness to normal

In other words: It toggles between black screen and normal brightness.

How to use

  • Copy the script into an empty file, save it as toggle_screen.py

  • Run it by the command:

      python3 /path/to/toggle_screen.py
    
  • Make sure you can repeat the command with a black screen, or else you will have to log out to toggle back...

  • If it works as you'd like it to, add it to a keyboard shortcut: Choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command.

The script

#!/usr/bin/env python3

import subprocess

cmd1 = "xrandr --verbose" get = subprocess.check_output(["/bin/bash", "-c", cmd1]).decode("utf-8").split() brightness = get[get.index("Brightness:")+1] screens = [get[i-1] for i in range(len(get)) if get[i] == "connected"]

if brightness == "1.0": for scr in screens: subprocess.Popen(["/bin/bash", "-c", "xrandr --output "+scr+" --brightness 0"]) else: for scr in screens: subprocess.Popen(["/bin/bash", "-c", "xrandr --output "+scr+" --brightness 1"])

Jacob Vlijm
  • 83,767
  • @JacobVlijm --- really nice. I like it. The only problem is that if you have two screens it "shuts off" just one. Maybe searching for "connected" (the screen name is just before) and if brightness is found after that, shut it off too? (Moreover --- you are not using the brightness value --- you just set it full after switching, which is mostly ok, but...) – Rmano Dec 09 '14 at 13:24
  • 1
    @Rmano Thanks for the tip! edited it into the answer. – Jacob Vlijm Dec 09 '14 at 13:45
  • 1
    Works ok. Nice! Renaming to enters_student_while_editing_test.py ;-) @jmrf: please read http://askubuntu.com/help/someone-answers – Rmano Dec 09 '14 at 15:36
  • xset dpms force standby seems to be more reliable and doesn't need the sleep 1 ; xset dpms force off trick. – Alan Thompson Sep 17 '15 at 03:59