Turn your laptop screen off with a keyboard shortcut in Ubuntu Karmic

裝了ubuntu 10.04 發現xset dpms force off有一點問題,

xset dpms force off可以關閉屏幕但是一會儿屏幕就會亮了,

查一下資料說是跟gnome-power-manger有頭,所以在xset dpms force off前

用killall  gnome-power-manger就可以了。

如果一定要使用gnome-power-manger可參考下面的文章(關閉屏幕要使用一個快捷鍵,啟用屏幕也用一個快捷鍵)

 

Back in the pre-Karmic days (or, the good ole’ days, as I call them) you could turn your laptop screen off by issuing this simple command:

user@computer:$ xset dpms force off

I had a nice script set up and mapped to a keyboard shortcut to turn my screen off whenever I wanted (say if I was using my laptop to play music and wanted to save some battery life). In Karmic, however, a regression causes this command to turn your screen off only briefly. After a few seconds, your screen will turn back on, on its own. This might be an Intel-specific problem—I have the misfortune of having an Intel video card on my Ubuntu install—but I’m not sure.

To get around this problem, I created a slightly different (and slightly less ideal) script that turns the screen off when a shortcut key is pressed, and then turns it back on after they same key is pressed again. Here’s how to get it for your own laptop:

  1. Create a new file using Nano. I put it in a directory called .scripts, you can put it wherever you like:
    user@computer:$ nano ~/.scripts/screen-off
  2. Paste the following into your new file using ctrl+shift+v :
    #!/bin/bash
    screenOffLockFile = / tmp / screen-off-lock

    if [ -f $screenOffLockFile ] ;
    then
            rm $screenOffLockFile
    else
            touch $screenOffLockFile
            sleep .5
            while [ -f  $screenOffLockFile ]
            do
                    xset dpms force off
                    sleep 2
            done
            xset dpms force on
    fi

    (An explanation follows). Press ctrl+x to quit Nano, then y to save your script.

  3. Make the script executable:
    user@computer:$ chmod u+x ~/.scripts/screen-off
  4. Open the System menu, go to Preferences, then Keyboard Shortcuts (or press alt+f2 ) and run gnome-keybinding-properties.
  5. Click Add, enter ‘Turn Off Screen’ under Name and ‘~/.scripts/screen-off’ (without quotes) under Command.
  6. After you’ve added the command, it will appear under the Custom Shortcuts header. Click on your new command to assign it a shortcut key of your choice (I use ctrl+alt+shift+s ).

And that’s it. Press your shortcut key to turn your screen off, and press the shortcut key again to turn it back on.

What the script does

When you first press the shortcut key with your screen on, the script will check if there’s a certain file in your /tmp/ directory. If it doesn’t find it, it will create it and then enter an infinite loop that turns your screen off every 2 seconds if the special file still exists (thus working around Karmic’s regression). When you press the shortcut key with your screen already off, it will delete the file and do nothing else, causing the first instance of the script to exit its infinite loop (since it no longer sees the special file) and turn your screen back on again.

It’s as simple as that. It’s not as ideal as the pre-Karmic solution, but it’ll have to do until Canonical QA gets their act together.

你可能感兴趣的:(Turn your laptop screen off with a keyboard shortcut in Ubuntu Karmic)