📜 ⬆️ ⬇️

Turning off the monitor with a hot key



It is a pity that the ability to turn off the monitor with a hot key in the usual way broke in one of the previous versions of Ubuntu. A simple nice script can return this necessary functionality (presumably to owners of laptops) for a few simple gestures.

As many probably know, the old way to turn off a monitor is to use the xset command :

xset dpms force off

In general, there are several variations on this topic, but at least start from Ubuntu Karmic (9.10), this feature has been broken. Some system calls do not seem to do very well with this command, causing the screen to wake up in about a minute. This, of course, caused inconvenience for some time, until I found for myself a method worthy of use. On ubuntuorums, it was proposed to use this command in a cycle, which naturally increased the utilization of the processor, while other methods were not so elegant. But thanks to the user nxmehta, a solution was found - namely, to use Python to achieve the goal. The script that was written works at least on releases from Karmic by Natty.
')
To begin with, you should install, besides the python itself, the python-xlib package. To do this, it is enough to execute the command in the terminal:

sudo apt-get install python python-xlib

The next thing you need to do is open a text editor (for example, gedit) and copy the following code:

  1. #! / usr / bin / python
  2. import time
  3. import subprocess
  4. from Xlib import X
  5. from Xlib.display import Display
  6. display = Display ( ': 0' )
  7. root = display.screen (). root
  8. root.grab_pointer (True,
  9. X.ButtonPressMask | X.ButtonReleaseMask | X.PointerMotionMask,
  10. X.GrabModeAsync (X.GrabModeAsync, 0, 0, X.CurrentTime)
  11. root.grab_keyboard (True,
  12. X.GrabModeAsync (X.GrabModeAsync, X.CurrentTime)
  13. subprocess.call ( 'xset dpms force off' .split ())
  14. p = subprocess.Popen ( 'gnome-screensaver-command -i' .split ())
  15. time.sleep (1)
  16. while true:
  17. print display.next_event ()
  18. p.terminate ()
  19. break
* This source code was highlighted with Source Code Highlighter .


What he does - I think you should not stop because of its simplicity.


Save your file somewhere, giving the desired name. I have developed a habit for executable scripts to use the bin folder, which is stored in my home folder. By the way, the scripts in this folder will be "visible" without specifying the absolute path. All thanks to the simple lines in the ~ / .profile file:
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi

So, the file ~ / bin / screen_off.py took place next to my other scripts.

The next step is to make it executable. You can do this quickly with chmod + x ~ / bin / screen_off.py , but if you don’t know how to use the console yet (for good reason), you can use the graphic part of the shell:
Right-click on the file and select Properties:

On the Permissions tab and check the Allow executing file as “Allow executing file as program” checkbox.

Now you can assign any keyboard shortcut to this script. I chose Caps Lock, but if you have curved fingers you prefer to use another key - no problem. In my case, I need to first deactivate the use of this key. This can be done through the menu Keyboard> Layouts> Layout Options (Keyboard> Layouts> Options):

This is where you can turn it off by searching for and selecting the Caps Lock key behavior and unchecking the corresponding box.

However, all this can also be done without difficulty, without resorting to the graphical shell through gconftool-2, as, for example, with me:
$ # Caps Lock
$ xmodmap -e "remove lock = Caps_Lock"
$ # :
$ gconftool-2 -a /desktop/gnome/keybindings/custom0
binding = VoidSymbol
action = /home/your_username/bin/screen_off.py
name = Screen_Off


Finally, to assign a script to a key, open the Keyboard Shortcuts (Keyboard Shortcuts) dialog.

Click Add, give any name to the keyboard shortcut, and the command is the path to the file created. Click Apply and then click on the Shortcut key field, then press the desired shortcut key. If you deactivate Caps Lock, you will see the text “VoidSymbol”.

Everything! Click! I hope this method is useful in order to save a little battery of your laptop.

Source: https://habr.com/ru/post/118625/


All Articles