May 22

How to switch off your screen with a keyboard shortcut

Category: Linux   — Published by goeszen on May 22, 2015 at 12:45 pm

Recently, I was annoyed by the way my display went to sleep (power off/ stand-by mode) on Ubuntu / Linux. I have no screensaver installed so everything what happened was that after a few minutes of idling, the display would go to sleep (into stand by). But I had no means to speed this up, for example when I knew I'd be away for some time, this way, wasting a few minutes of power.

As it turns out, from command-line, you can issue the "go to sleep" command manually:

xset dpms force off

Now, on Ubuntu, you can easily define a custom Keyboard-Shortcut for this command:
Go to System > Keyboard > Shortcuts, and define / assocaited a new personal shortcut and enter "xset dpms force off" as the command. Then
define a keyboard shortcut for it to fire, for example I chose "CTRL" + "<" ...which happen to be next to each other on a QWERTZ keyboard.

... or switch your display off by clicking a desktop icon

Open terminal, and do this:

$ nano ~/Desktop/xoff.desktop

Paste this into the file:

#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Icon[en_EN]=disable-device
Name[en_EN]=xoff
Comment[en_EN]=switch off / turn off screen
Exec=xset dpms force off
Name=xoff
Comment=switch off / turn off screen
Icon=disable-device

and add the executable bit:

$ chmod ogw+x xoff.desktop

Update:
I found that firing the xset command, under certain circumstances which I don't understand, doesn't stick and is immediately overridden, so the screen lights up again right after going black. It could be that this happens with DRAW events on the X GUI canvas (as it's when some GUI application is doing animated updates), something like that.
Anyway, firing the xset command over and over seems to be the only solution. I was so annoyed by this, mashing the shortcut butoons until the screen remained asleep, that I wrote a small Perl script which I fire via the shortcut now, wrapping the xset command. And so far, this works great:

#!/usr/bin/perl
# xoff.pl

use Time::HiRes;

my $sleep = 0.3;
for(1..6){
system("xset dpms force off");

# print "Fired xset, sleep $sleep \n";
Time::HiRes::sleep($sleep);
$sleep += 0.2;
}

Leave a Reply

=