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).
1 Answers
There are two circumstances that make your situation a bit different from the supposed duplicate(s):
- 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. - 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"])

- 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
-
1Works 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 thesleep 1 ; xset dpms force off
trick. – Alan Thompson Sep 17 '15 at 03:59
xset dpms force off
. To turn it on again: touch any key. – Jacob Vlijm Dec 09 '14 at 10:33