Jump to content

Turn off the monitor when power button pressed


Recommended Posts

hi all

i hade made my Laptop do nothing when i press the power button

i want to make a script that turn off the monitor when i press the power button (i can't find that option in my windows)

i tried using _is pressed an Hotkeyset but i can't find the ascii code of the power button

any ideas ?

Link to comment
Share on other sites

There isn't a keycode for the power button. Windows responds to the hardware when you press it.

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

You might find something that utilizes WinAPI's PowerCreateSetting function to do that, or something similar.

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

If you set the power button option to standby/sleep, you should be able to intercept that command, run your own, then deny the power event.

Global Const $WM_POWERBROADCAST = 0x0218
Global Const $PBT_APMRESUMESTANDBY = 0x0008
Global Const $BROADCAST_QUERY_DENY = 0x424D5144

$hGUI = GUICreate("Standby test", 100, 100, 1, 1)
GUIRegisterMsg($WM_POWERBROADCAST, "Standby")

While 1
    Sleep(10)
WEnd

Func Standby($hWnd, $Msg, $wParam, $lParam)
    If $wParam = $PBT_APMRESUMESTANDBY Then
        DoSomething()
        ;Cancel standby
        Return $BROADCAST_QUERY_DENY
    EndIf
EndFunc   ;==>Standby

Func DoSomething()
    ;Turn off screen or whatever (non-blocking)
EndFunc   ;==>DoSomething

Untested and compiled from the following threads:

'?do=embed' frameborder='0' data-embedContent>>

'?do=embed' frameborder='0' data-embedContent>>

'?do=embed' frameborder='0' data-embedContent>>

Link to comment
Share on other sites

 
 
thank for replay bro
 
for now i'm using your script but it can't interrupt the sleep then msgbox 
i'm i missing something ?
Global Const $WM_POWERBROADCAST = 0x0218
Global Const $PBT_APMRESUMESTANDBY = 0x0008
Global Const $BROADCAST_QUERY_DENY = 0x424D5144

$hGUI = GUICreate("Standby test", 100, 100, 1, 1)
GUIRegisterMsg($WM_POWERBROADCAST, "Standby")

While 1
    Sleep(10)
WEnd

Func Standby($hWnd, $Msg, $wParam, $lParam)
    If $wParam = $PBT_APMRESUMESTANDBY Then
        DoSomething()
        ;Cancel standby
        Return $BROADCAST_QUERY_DENY
    EndIf
EndFunc   ;==>Standby

Func DoSomething()
MsgBox(0, "", "func")
EndFunc   ;==>DoSomething

 

Link to comment
Share on other sites

 

 
 
thank for replay bro
 
for now i'm using your script but it can't interrupt the sleep then msgbox 
i'm i missing something ?
Global Const $WM_POWERBROADCAST = 0x0218
Global Const $PBT_APMRESUMESTANDBY = 0x0008
Global Const $BROADCAST_QUERY_DENY = 0x424D5144

$hGUI = GUICreate("Standby test", 100, 100, 1, 1)
GUIRegisterMsg($WM_POWERBROADCAST, "Standby")

While 1
    Sleep(10)
WEnd

Func Standby($hWnd, $Msg, $wParam, $lParam)
    If $wParam = $PBT_APMRESUMESTANDBY Then
        DoSomething()
        ;Cancel standby
        Return $BROADCAST_QUERY_DENY
    EndIf
EndFunc   ;==>Standby

Func DoSomething()
MsgBox(0, "", "func")
EndFunc   ;==>DoSomething

 

Looks like I was using the wrong event there.  Sleep on power button triggers 0x0004 which is PBT_APMSUSPEND

This works to rigger the DoSomething() function, but it seems that the $BROADCAST_QUERY_DENY is not actually stopping the suspend. I'm going to play around with it a bit more when time allows.  Sorry it took me so long to test, but I'm at work.

Global Const $WM_POWERBROADCAST     =     0x0218
Global Const $PBT_APMRESUMESTANDBY     =     0x0008
Global Const $PBT_APMSUSPEND         =     0x0004
Global Const $BROADCAST_QUERY_DENY     =     0x424D5144

$hGUI = GUICreate("Standby test", 100, 100, 1, 1)
GUIRegisterMsg($WM_POWERBROADCAST, "Standby")

While 1
    Sleep(10)
WEnd

Func Standby($hWnd, $Msg, $wParam, $lParam)
    ConsoleWrite("test1 - " & $wParam & @CRLF)
    If $wParam = $PBT_APMSUSPEND Then
        DoSomething()
        ;Cancel standby
        Return $BROADCAST_QUERY_DENY
    EndIf
    Return $BROADCAST_QUERY_DENY
EndFunc   ;==>Standby

Func DoSomething()
    ;Turn off screen or whatever (non-blocking)
    ConsoleWrite("test2" & @CRLF)
EndFunc   ;==>DoSomething

The above works to trigger the function when the power button is set to sleep and pressed, however it fails to actually stop the sleep as it stands now.  I'll look more in to this when I can.

Looks like we are not alone in seeing this behavior in Windows 7: http://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/e711f471-0ae8-4fde-b333-5e369a5283c3/broadcastquerydeny-does-not-prevent-sleep-in-windows-7

 

EDIT: Looks like this is not possible for a sleep event anymore as of Windows 7: http://stackoverflow.com/questions/14433874/how-to-detect-power-button-event-and-deny-it-on-windows-7-from-laptop

You could change this to a shutdown event for Windows 7 and be successful, but this would also effect when you use the start menu to actually try and shut down.  You could always close the program if you really wanted to shut down or code a hotkey that doesn't pass BROADCAST_QUERY_DENY if the hotkey is being held, etc.  That's the only way you are going to be able to hook the power button in Windows 7 and still deny the action, as far as I know.

Edited by danwilli
Link to comment
Share on other sites

Looks like I was using the wrong event there.  Sleep on power button triggers 0x0004 which is PBT_APMSUSPEND

This works to rigger the DoSomething() function, but it seems that the $BROADCAST_QUERY_DENY is not actually stopping the suspend. I'm going to play around with it a bit more when time allows.  Sorry it took me so long to test, but I'm at work.

Global Const $WM_POWERBROADCAST     =     0x0218
Global Const $PBT_APMRESUMESTANDBY     =     0x0008
Global Const $PBT_APMSUSPEND         =     0x0004
Global Const $BROADCAST_QUERY_DENY     =     0x424D5144

$hGUI = GUICreate("Standby test", 100, 100, 1, 1)
GUIRegisterMsg($WM_POWERBROADCAST, "Standby")

While 1
    Sleep(10)
WEnd

Func Standby($hWnd, $Msg, $wParam, $lParam)
    ConsoleWrite("test1 - " & $wParam & @CRLF)
    If $wParam = $PBT_APMSUSPEND Then
        DoSomething()
        ;Cancel standby
        Return $BROADCAST_QUERY_DENY
    EndIf
    Return $BROADCAST_QUERY_DENY
EndFunc   ;==>Standby

Func DoSomething()
    ;Turn off screen or whatever (non-blocking)
    ConsoleWrite("test2" & @CRLF)
EndFunc   ;==>DoSomething

The above works to trigger the function when the power button is set to sleep and pressed, however it fails to actually stop the sleep as it stands now.  I'll look more in to this when I can.

Looks like we are not alone in seeing this behavior in Windows 7: http://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/e711f471-0ae8-4fde-b333-5e369a5283c3/broadcastquerydeny-does-not-prevent-sleep-in-windows-7

 

EDIT: Looks like this is not possible for a sleep event anymore as of Windows 7: http://stackoverflow.com/questions/14433874/how-to-detect-power-button-event-and-deny-it-on-windows-7-from-laptop

You could change this to a shutdown event for Windows 7 and be successful, but this would also effect when you use the start menu to actually try and shut down.  You could always close the program if you really wanted to shut down or code a hotkey that doesn't pass BROADCAST_QUERY_DENY if the hotkey is being held, etc.  That's the only way you are going to be able to hook the power button in Windows 7 and still deny the action, as far as I know.

 

so there is no way to do that even if it was not autoit ?

Link to comment
Share on other sites

so there is no way to do that even if it was not autoit ?

To be specific, in Windows 7, there is no way to interrupt PBT_APMSUSPEND with BROADCAST_QUERY_DENY like in previous versions of Windows.

I see lots of forums in all different programming languages discussing the same.

As for another way to accomplish the same thing, there probably is, but I don't know what it is.

Maybe if you changed the power button to shut down and attempt to interrupt that with BROADCAST_QUERY_DENY, but then you wouldn't be able to shut down the computer from the start menu either unless you looked for something like a hotkey and didn't pass the BROADCAST_QUERY_DENY if it was held down, or made your own shutdown option in your script, or shut down the app first, or any other not very clean/professional way.

Maybe somebody that knows a lot more than I do will join the thread and show us the light.  If the shutdown option seems fine to you, you can give that a shot though.

EDIT: I tested stopping the shutdown, and it works, but it really only holds the shutdown from occurring on Windows 7, and will still show the shutdown screen until you hit cancel.  So for Windows 7, I have no idea how to hook the power button as this method will not work for it.

Edited by danwilli
Link to comment
Share on other sites

Use a hotkey to trigger a function that does the job. It's far less troublesome that way.

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

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...