SEARCH

How-To Geek

How To Turn Off Your Monitor With a Hotkey in Ubuntu

banner-01

The traditional way of turning your monitor off via a hotkey has been broken for a few versions now. A pretty simple Python script can bring that functionality back reliably and efficiently.

The old way of turning off you monitor was through the xset command:

xset dpms force off

There are a few variations on that, but since Ubuntu Karmic (9.10), it’s been broken. Some of the system calls apparently don’t get along well with this command, causing the screen to wake up after about a minute. After being annoyed at this for quite some time, I found a few solutions on the Ubuntu Forums. Running this command in a loop seems CPU intensive, and there isn’t a very elegant way to escape it. Thankfully, one user, nxmehta, found a solution utilizing a simple Python script, and it works on everything from Karmic to Natty.

First of all, you need to have a few dependencies, so open up a terminal and enter the following command:

sudo apt-get install python python-xlib

This will install the python and python-xlib packages if they aren’t installed already. Next, open up Text Editor (gedit) and copy/paste the following text:

#!/usr/bin/python

import time
import subprocess
from Xlib import X
from Xlib.display import Display

display = Display(':0')
root = display.screen().root
root.grab_pointer(True,
        X.ButtonPressMask | X.ButtonReleaseMask | X.PointerMotionMask,
        X.GrabModeAsync, X.GrabModeAsync, 0, 0, X.CurrentTime)
root.grab_keyboard(True,
        X.GrabModeAsync, X.GrabModeAsync, X.CurrentTime)

subprocess.call('xset dpms force off'.split())
p = subprocess.Popen('gnome-screensaver-command -i'.split())
time.sleep(1)

while True:
    print display.next_event()
    p.terminate()
    break

gedit

Save your file somewhere with a proper name. I stuck mine in ~/bin/screen_off.sh with the rest of my scripts.

Next, right-click the file and go to Properties.

properties

Under the Permissions tab, be sure “Allow executing file as program” is checked. Click Close.

Now you can assign it to any keyboard shortcut! I like to set mine to Caps Lock, so I had to disable that key first. You can do that by going to Keyboard > Layouts > Options.

keyboard layout options

Here, choose “Caps Lock is disabled” under Caps Lock key behavior.

To assign this script to a keyboard shortcut, open up Keyboard Shortcuts preferences.

shortcuts

Click Add, give the shortcut a name, and the command will just be the location of the script. Click Apply, then click under shortcut to set it. If you disabled Caps Lock, you’ll see it listed as “VoidSymbol” but it will work without a hitch.

This has been bugging me for quite some time, and the solution was a bit tough to find what with all of the complaints floating around. Hopefully this will help you conserve your laptop battery.

Yatri Trivedi is a monk-like geek. When he's not overdosing on meditation and geek news of all kinds, he's hacking and tweaking something, often while mumbling in 4 or 5 other languages.

  • Published 05/3/11

Comments (11)

  1. kun

    Thanks for the tip. Useful…

  2. Benjamin Cahill

    Wow, I’m glad I don’t use Ubuntu.

    Arch all the way! :-P

  3. Freak Andelle

    Great tip indeed! This has been bugging me for quite some time too now…

  4. Rob M

    I Like .. I Like .. I Like .. I Like .. many Thanks :-)

  5. Norbert Rehaut

    Is there a hot key

    Can I turn Off my Monitor With a Hotkey in Win XP Pro?

  6. codegirl

    I changed it from “gnome-screensaver-command -i” to “gnome-screensaver-command -l” so it would lock my computer as well. Thanks for the great tip!

  7. xana452

    Cool, but is there a way for this in Windows 7 Home Premium? If so, please throw one up here!

  8. aj

    In Ubuntu 10.10 display comes back after 2-3 seconds .. :(

    Help?

  9. Eric-Sebastien Lachance

    First, AWESOME. Thank you very much. I’m not exactly comfortable in Linux and your instructions were really clear and simple. This works perfectly in 11.04

    aj, I have that issue sometimes too. Usually if I open a window (like my “Home Folder”) and do it again it works fine… Or it may be because “letting go” of the key sometimes triggers the screensaver to exit. Not sure.

    And if you’re in Windows and you need something similar to this, there’s a tiny executable called “Turn off LCD.exe” that you can find and download, I use it in Windows 7 and it works great. As for shortcut keys, you’ll have to figure that one out yourself (I just pinned it to the taskbar and click it).

  10. Stijn Verwaaijen

    Nothing has changed. The monitor keeps turning back on… Please help!

  11. Jae

    Thank you! I’ve always wanted to do this.

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!