Jump to content

Turn off laptop when you close the lid or push power button (resolved)


Recommended Posts

I need to turn off my notebook every time the cover is closed, does anyone know how to identify when the notebook's cover has been closed?

Edited by Belini
Link to comment
Share on other sites

  • Moderators

Why not just use power settings to do this? Under options for closing the lid (Windows 7 and above) there is an option to shut the device down

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

I wanted to do it by code, ie to monitor the cover and turn it off when it is closed, so that I do not have to configure all the notebook's that I'm going to use the program!

Link to comment
Share on other sites

@Earthshine I tried to edit the registry following your advice but it did not work, I need it to look like this:

- Power off when you press the power button
- off when you close the cover

 

Link to comment
Share on other sites

You want to be able to do this to OTHER people's computers? Are you giving them the option to not let you do this when they run your program?

I know I'd be PISSED if your program did this to my computer without telling me.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

1 hour ago, Belini said:

@Earthshine I tried to edit the registry following your advice but it did not work, I need it to look like this:

- Power off when you press the power button
- off when you close the cover

 

did you reboot? or at least log off and on again after making registry change?

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

1 hour ago, BrewManNH said:

You want to be able to do this to OTHER people's computers? Are you giving them the option to not let you do this when they run your program?

I know I'd be PISSED if your program did this to my computer without telling me.

They are all notebooks of mine that will only be used to run my program and nothing else, will not have taskbar, explorer and nor to the task manager!

1 hour ago, Earthshine said:

oh and i found this

 

@Earthshine is now turning it off when the lid is closed, thanks for the help!

Run("powercfg.exe -SETACVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 3", "",@SW_HIDE)
Run("powercfg.exe -SETDCVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 3", "",@SW_HIDE)

Now to be perfect, i only need to know how to turn it off when the power button is pressed!

 

 

Link to comment
Share on other sites

I was able to find it here: https://social.msdn.microsoft.com/Forums/en-US/06bdec1d-4ffa-4a4a-b381-9265c304cc85/command-line-configuration-of-power-button-function?forum=quebecmisc

Run("powercfg.exe -SETACVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 3", "C:\WINDOWS\system32", @SW_HIDE)
Run("powercfg.exe -SETDCVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 3", "C:\WINDOWS\system32", @SW_HIDE)

Run("powercfg.exe -powercfg.exe -SETACVALUEINDEX 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c 4f971e89-eebd-4455-a8de-9e59040e7347 7648efa3-dd9c-4e3e-b566-50f929386280 3", "C:\WINDOWS\system32", @SW_HIDE)
Run("powercfg.exe -powercfg.exe -SETDCVALUEINDEX 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c 4f971e89-eebd-4455-a8de-9e59040e7347 7648efa3-dd9c-4e3e-b566-50f929386280 3", "C:\WINDOWS\system32", @SW_HIDE)

 

Link to comment
Share on other sites

I know you already found your answer.  I thought it would be helpful to show a function that sets multiple power options, including what you wanted.    

  

Func _SetPowerOptions() ;Set multiple power options.

    ;Set the current power plan standby timeout ("Put the computer to sleep") to "Never".
    Local $iPowercfgReturn = RunWait("powercfg -x standby-timeout-ac 0", '', @SW_HIDE)
    If @error Or $iPowercfgReturn <> 0 Then Return SetError(1, 0, False)

    ;Set monitor timeout ("Turn off the display") to 30 minutes.
    $iPowercfgReturn = RunWait("powercfg -x monitor-timeout-ac 30", '', @SW_HIDE)
    If @error Or $iPowercfgReturn <> 0 Then Return SetError(2, 0, False)

    Local $s64Bit = ""
    If @OSArch = "X64" Then $s64Bit = "64"
    
    ;Disable Hibernate.
    Local Const $sRegKeyPower = "HKEY_LOCAL_MACHINE" & $s64Bit & "\SYSTEM\CurrentControlSet\Control\Power"
    Local $iRegWriteReturn = RegWrite($sRegKeyPower, "HibernateEnabled", "REG_DWORD", 0)
    If @error Or $iRegWriteReturn <> 1 Then Return SetError(3, @error, False)

    Local Const $sRegKeyPowerSettings = "HKEY_LOCAL_MACHINE" & $s64Bit & "\SOFTWARE\Policies\Microsoft\Power\PowerSettings"
    
    ;Disable Sleep in the Start Menu.
    Local Const $sRegKeyPowerSettingsSleepStartMenu = $sRegKeyPowerSettings & "\abfc2519-3608-4c2a-94ea-171b0ed546ab"
    $iRegWriteReturn = RegWrite($sRegKeyPowerSettingsSleepStartMenu, "ACSettingIndex", "REG_DWORD", 0)
    If @error Or $iRegWriteReturn <> 1 Then Return SetError(4, @error, False)
    $iRegWriteReturn = RegWrite($sRegKeyPowerSettingsSleepStartMenu, "DCSettingIndex", "REG_DWORD", 0)
    If @error Or $iRegWriteReturn <> 1 Then Return SetError(5, @error, False)

    ;Set Sleep Button to "Do Nothing"
    Local Const $sRegKeyPowerSettingsSleepButton = $sRegKeyPowerSettings & "\96996BC0-AD50-47EC-923B-6F41874DD9EB"
    $iRegWriteReturn = RegWrite($sRegKeyPowerSettingsSleepButton, "ACSettingIndex", "REG_DWORD", 0)
    If @error Or $iRegWriteReturn <> 1 Then Return SetError(6, @error, False)
    $iRegWriteReturn = RegWrite($sRegKeyPowerSettingsSleepButton, "DCSettingIndex", "REG_DWORD", 0)
    If @error Or $iRegWriteReturn <> 1 Then Return SetError(7, @error, False)

    ;Set Power Button to "Shut down"
    Local Const $sRegKeyPowerSettingsPowerButton = $sRegKeyPowerSettings & "\7648EFA3-DD9C-4E3E-B566-50F929386280"
    $iRegWriteReturn = RegWrite($sRegKeyPowerSettingsPowerButton, "ACSettingIndex", "REG_DWORD", 3)
    If @error Or $iRegWriteReturn <> 1 Then Return SetError(8, @error, False)
    $iRegWriteReturn = RegWrite($sRegKeyPowerSettingsPowerButton, "DCSettingIndex", "REG_DWORD", 3)
    If @error Or $iRegWriteReturn <> 1 Then Return SetError(9, @error, False)

    ;Set "When I close the lid" to "Shut down"
    Local Const $sRegKeyPowerSettingsCloseLid = $sRegKeyPowerSettings & "\5CA83367-6E45-459F-A27B-476B1D01C936"
    $iRegWriteReturn = RegWrite($sRegKeyPowerSettingsCloseLid, "ACSettingIndex", "REG_DWORD", 3)
    If @error Or $iRegWriteReturn <> 1 Then Return SetError(10, @error, False)
    $iRegWriteReturn = RegWrite($sRegKeyPowerSettingsCloseLid, "DCSettingIndex", "REG_DWORD", 3)
    If @error Or $iRegWriteReturn <> 1 Then Return SetError(11, @error, False)

    Return True
EndFunc   ;==>_SetPowerOptions

 

Adam

Link to comment
Share on other sites

Thanks @AdamUL will be very useful this information to change directly in the registry too!

Link to comment
Share on other sites

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...