Assign a shortcut key to turn off your monitor, to save battery life on your laptop

Sep 24, 2011 13:01 GMT  ·  By

This tutorial will teach Linux users how to make a script in order to easily turn off their netbook or laptop's monitor (LCD screens) with a keystroke, to save battery life.

Are you using Linux and you want to save the life of your laptop/netbook's battery? Look no further, we have the solution for you! The following guide will teach you step-by-step with screenshots, how to make a script and create a shortcut key in order to easily turn off your LCD screen with a keystroke, to save the life of your notebook's battery.

We've tested this tutorial on the newly released Ubuntu 11.10 Beta 2 (Oneiric Ocelot) operating system and it's working like a charm. Please follow the instructions below carefully.

Step 1 - Install the requirements

First of all, we need to install a Python library, so just click the link below and let your package manager install the required package. After installation, close the package manager and proceed to the next step.

Install python-xlib

Step 2 - Make the script

Now that you've installed the required library (see above), all you need to do is to right click on your desktop and select the "Create New Document -> Empty Document" option.

Review image
Rename the file to monitor_off.sh. Now, open it with a double click, copy the code below and paste it in.

#!/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 Save the file!

Review image
Right click on the monitor_off.sh file and choose "Properties".
Review image
Go to the "Permissions" tab and check the "Allow executing file as program" option.
Review image
Editor's note: Place the monitor_off.sh file somewhere on your home folder, but remember the path for the next step.

Step 3 - Make the shortcut

Click the Ubuntu logo to open the Unity Dash and search "keyboard" (without quotes). Click the "Keyboard" icon and the Keyboard app will open.

Review image
Go to the "Shortcuts" tab, select "Custom Shortcuts" option from the list and click the + button to add a new shortcut. In the "Name:" field write something like "Monitor Off Script" and in the command field write the path to the script, including the name of the script (e.g. /home/softpedia/monitor_off.sh). Click the "Apply" button when you're done.
Review image
To assign a keyboard shortcut, click the "Disabled" entry on the newly created shortcut line and press the desired key on your keyboard, when it says "New shortcut".
Review image
We've assigned F10 as the shortcut key for our monitor off script.
Review image
That's it! Close the Keyboard app and press the selected shortcut key to test the script. Is your monitor off now? Turn it back on with the Esc key or with a mouse click.

If you encounter issues with the tutorial, do not hesitate to drop a comment below. Have fun using Linux!