Power managment in new version of Windows (8.1, 10)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
taravasya
Posts: 12
Joined: 01 Mar 2016, 08:50

Power managment in new version of Windows (8.1, 10)

02 Mar 2016, 15:47

Hello again! Its my last hope... :cry:
I was searching for couple days for answear of how-to power managment(events of standby or hibernate) to handle in Win8.1 or Win10?
I found many of examples for it around such this:

Code: Select all

OnMessage(0x218, "WM_POWERBROADCAST")
WM_POWERBROADCAST(wparam, lparam)
{
;some code here...
}
But... as I understand, this method does not work any more for newly versions of windows.
So.... How I can, for example, catch a message about resume from connected stanby? Pleeeeaaassse..... :cry: :cry: :cry:
lexikos
Posts: 9621
Joined: 30 Sep 2013, 04:07
Contact:

Re: Power managment in new version of Windows (8.1, 10)

02 Mar 2016, 18:49

It works just fine.

This code logs "PBT_APMSUSPEND" when I put the Windows 10 PC to sleep, and "PBT_APMRESUMESUSPEND" when I wake it up.

Code: Select all

OnMessage(0x218, "WM_POWERBROADCAST")
WM_POWERBROADCAST(wparam, lparam)
{
	static changeText := {10: "PBT_APMPOWERSTATUSCHANGE", 18: "PBT_APMRESUMEAUTOMATIC", 7: "PBT_APMRESUMESUSPEND", 4: "PBT_APMSUSPEND", 32787: "PBT_POWERSETTINGCHANGE"}
	change := changeText[wparam]
	if !change
		change := wparam
	FormatTime now
	FileAppend %now% :: %change%`n, log.txt
}
taravasya
Posts: 12
Joined: 01 Mar 2016, 08:50

Re: Power managment in new version of Windows (8.1, 10)

02 Mar 2016, 19:39

Thanks for reply! Sorry...
I create hext script:

Code: Select all

Gui, New
Gui, Add, Text,, This is text:
Gui, Show
OnMessage(0x218, "WM_POWERBROADCAST")
WM_POWERBROADCAST(wparam, lparam)
{
	static changeText := {10: "PBT_APMPOWERSTATUSCHANGE", 18: "PBT_APMRESUMEAUTOMATIC", 7: "PBT_APMRESUMESUSPEND", 4: "PBT_APMSUSPEND", 32787: "PBT_POWERSETTINGCHANGE"}
	change := changeText[wparam]
	if !change
		change := wparam
	FormatTime now
	FileAppend %now% :: %change%`n, log.txt
}
and with it I don`t have a logs in log.txt file(I create them with myself).
I have installed Win8.1 on my tablet. Maybe trouble in this?

And one more thing. On win-tablets provided Connected Standby wich may be different...
lexikos
Posts: 9621
Joined: 30 Sep 2013, 04:07
Contact:

Re: Power managment in new version of Windows (8.1, 10)

02 Mar 2016, 20:08

Apparently you have to opt in if connected standby is enabled. Try adding this to the top of your script:

Code: Select all

DllCall("RegisterSuspendResumeNotification", "ptr", A_ScriptHwnd, "uint", 0)
taravasya
Posts: 12
Joined: 01 Mar 2016, 08:50

Re: Power managment in new version of Windows (8.1, 10)

03 Mar 2016, 09:58

lexikos wrote:Try adding this
Its help! Thank you :wave:
But unfortunately, in the end it did not solve my problem.
I need to run a certain program directly before going to sleep, or immediately after exiting from sleep state.
In the first case, the command run is ignored and the tablet goes to sleep without performing the directive.
In the second case, the command run does not work as long as the system is unlocked (even if the system lock dissabled, after exiting sleep is displayed lock screen, and until it will be hide with touchscreen or mouse, the script does not continue execution).
So.. I need some other system message, what give me a little more time for execute my routines...
SifJar
Posts: 398
Joined: 11 Jan 2016, 17:52

Re: Power managment in new version of Windows (8.1, 10)

03 Mar 2016, 11:22

taravasya wrote:
lexikos wrote:Try adding this
Its help! Thank you :wave:
But unfortunately, in the end it did not solve my problem.
I need to run a certain program directly before going to sleep, or immediately after exiting from sleep state.
In the first case, the command run is ignored and the tablet goes to sleep without performing the directive.
In the second case, the command run does not work as long as the system is unlocked (even if the system lock dissabled, after exiting sleep is displayed lock screen, and until it will be hide with touchscreen or mouse, the script does not continue execution).
So.. I need some other system message, what give me a little more time for execute my routines...
You can disable the lock screen after sleep with a simple registry edit, see here: http://www.howtogeek.com/134620/how-to- ... up-policy/

As for running a command BEFORE sleep, the easiest solution I can think of is to create a hotkey using AHK which runs your command and then puts the computer to sleep. Of course, this isn't as universal as hooking a system message, as it would ONLY run if you sleep via your hotkey, not if you did e.g. Start > Power > Sleep. But if you were willing to try that, something like this should work:

Code: Select all

#s:: ;win + s for sleep
;insert command
DllCall("PowrProf\SetSuspendState", int, 0, int, 0, int, 0) ;sleep
return
There's probably a better solution than this though, not sure.
taravasya
Posts: 12
Joined: 01 Mar 2016, 08:50

Re: Power managment in new version of Windows (8.1, 10)

03 Mar 2016, 16:00

SifJar wrote:You can disable the lock screen after sleep with a simple registry edit
Thanks for the tips! With this registry keys I can do my job(I will be change them with my script, thus they will be edited only then I need it, and after this, they will be back to default value).
About custom action for go to standby, in my conditions it not good thing. I wanna standby my laptop just with hardware button on it.
Anyway thank you and goodluck! :D
SvenBent
Posts: 266
Joined: 09 Aug 2015, 01:34

Re: Power managment in new version of Windows (8.1, 10)

16 Mar 2016, 13:51

I'm having a similar issue and I cant figure out what part it I am missing
this is my code:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SetBatchLines -1


DllCall("RegisterSuspendResumeNotification", "ptr", A_ScriptHwnd, "uint", 0)
OnMessage(0x218, "WM_POWERBROADCAST")
Return

WM_POWERBROADCAST(wParam, lParam)
{
msgbox test W%wParam% L%lParam%
}
But the message box never gets activated.
I've tried manually going to sleep mode or just letting windows "timeout"
taravasya
Posts: 12
Joined: 01 Mar 2016, 08:50

Re: Power managment in new version of Windows (8.1, 10)

17 Mar 2016, 02:31

In function WM_POWERBROADCAST must be if statement. Something like this:

Code: Select all

WM_POWERBROADCAST(wParam, lParam)
{
  if wParam = 7
  {
     msgbox test W%wParam% L%lParam%
  }
}
and moreover you need to dissable lock screen:
the command run does not work as long as the system is unlocked (even if the system lock dissabled, after exiting sleep is displayed lock screen, and until it will be hide with touchscreen or mouse, the script does not continue execution).
You can disable the lock screen after sleep with a simple registry edit, see here: http://www.howtogeek.com/134620/how-to- ... up-policy/
Thanks for the tips! With this registry keys I can do my job(I will be change them with my script, thus they will be edited only then I need it, and after this, they will be back to default value).
I can share my full code in the evening.
SifJar
Posts: 398
Joined: 11 Jan 2016, 17:52

Re: Power managment in new version of Windows (8.1, 10)

17 Mar 2016, 06:06

taravasya wrote:In function WM_POWERBROADCAST must be if statement. Something like this:
Nonsense.
taravasya
Posts: 12
Joined: 01 Mar 2016, 08:50

Re: Power managment in new version of Windows (8.1, 10)

17 Mar 2016, 07:22

SifJar wrote:Nonsense.
Why?
I have this code(its only part from whole code):

Code: Select all

DllCall("RegisterSuspendResumeNotification", "ptr", A_ScriptHwnd, "uint", 0)
OnMessage(0x218, "WM_POWERBROADCAST")

;Here I Run My Some Code
;
;and after this I do reg-trick for NolockScreen after resume 
RegWrite, REG_DWORD, HKEY_LOCAL_MACHINE, SOFTWARE\Policies\Microsoft\Windows\Personalization, NoLockScreen, 1
;and after resume from suspend(0x07) i run my function:
WM_POWERBROADCAST(wparam, lparam)
	{
		if (wparam = 7)
		{
			;Here I againe Run My Some Code
			RegWrite, REG_DWORD, HKEY_LOCAL_MACHINE, SOFTWARE\Policies\Microsoft\Windows\Personalization, NoLockScreen, 0
		}
	}
Its worked... :wave:
taravasya
Posts: 12
Joined: 01 Mar 2016, 08:50

Re: Power managment in new version of Windows (8.1, 10)

17 Mar 2016, 07:35

OH! I see ))) I just was think only about MY CASE, then I was need to do my code only after resume from suspend... Sorry!
SvenBent
Posts: 266
Joined: 09 Aug 2015, 01:34

Re: Power managment in new version of Windows (8.1, 10)

17 Mar 2016, 10:14

@taravasya
Ty for the input, but im not trying to do exactly the same just trying to figureout to get this onmessage to work :D
I will try with your code just incase. I just cant figure out what I'm doing wrong

I'm working in windows 7 and 10 both 64 bit but nothing :headwall:
SifJar
Posts: 398
Joined: 11 Jan 2016, 17:52

Re: Power managment in new version of Windows (8.1, 10)

17 Mar 2016, 19:01

taravasya wrote:
SifJar wrote:Nonsense.
Why?
The function does not require an if statement to work. SvenBent's issue is that the function does not appear to be getting called at all, something which won't be solved simply by adding an if statement within the function.
SvenBent
Posts: 266
Joined: 09 Aug 2015, 01:34

Re: Power managment in new version of Windows (8.1, 10)

18 Mar 2016, 00:29

Nothing came out of your script Tara.
I didnt think so as as sifjar is saying its the actual call that seems not to be made. But at this time im willing to try anything short of going bareback on a prostitute...

Sifjar do you have any idea what it might be or what i'm doing wrong ?
SifJar
Posts: 398
Joined: 11 Jan 2016, 17:52

Re: Power managment in new version of Windows (8.1, 10)

18 Mar 2016, 09:57

SvenBent wrote:Sifjar do you have any idea what it might be or what i'm doing wrong ?
I'm afraid not, no. The code Lexikos posted works fine for me on Windows 10, not sure why it wouldn't be working for you.
SvenBent
Posts: 266
Joined: 09 Aug 2015, 01:34

Re: Power managment in new version of Windows (8.1, 10)

18 Mar 2016, 15:24

Figured it out. adding this line before the onemessage() fixed it

Gui +LastFound

Sadly what i wanted it for I cant solve this way :(
mentas
Posts: 2
Joined: 09 Nov 2016, 02:19

Re: Power managment in new version of Windows (8.1, 10)

09 Nov 2016, 11:37

Hello guys, I'm sorry my bad english ,I am talking through Google translator , your scripts work perfectly , but i can not adapt them to my task :
1. before entering the computer into sleep or hibernation, I want to hear SoundBeep, 750, 500
2. after resuming from sleep or hibernation, I want to hear SoundBeep, 400, 1500
that's all :D

I have a great working autoit script :

Code: Select all

#RequireAdmin
#pragma compile(Icon, "C:\Program Files (x86)\AutoIt3\Icons\au3script_v9.ico")
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_UseUpx=y
#AutoIt3Wrapper_Run_AU3Check=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <WindowsConstants.au3>
#include<Process.au3>
; <<condition for the transition to sleep>>
Global $PBT_APMSUSPEND = 0x0004
Global $PBT_APMRESUMESUSPEND = 0x0007
$hGUI = GUICreate("Restart", 100, 100, 1, 1)
GUIRegisterMsg($WM_POWERBROADCAST, "Standby")
While 1
    Sleep(100)
WEnd
Func Standby($hWnd, $Msg, $wParam, $lParam)
    Select
        ;<<We monitor the transition to sleep>>
        Case $wParam = $PBT_APMSUSPEND
            Suspend()
        ; <<monitor output from a sleep>>
        Case $wParam = $PBT_APMRESUMESUSPEND
            ResumeSuspend()
        Case Else
    EndSelect
EndFunc
; <<tasks before sleep >>
Func Suspend()
Beep(750, 1000)
EndFunc

; <<tasks after sleep >>
Func ResumeSuspend()
Sleep(500)
Beep(400, 1500)
EndFunc
but I need AHK script ,I do not have enough training , please help , I will be very grateful .

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, CoffeeChaton, mikeyww and 127 guests