| View previous topic :: View next topic |
| Author |
Message |
TheGood
Joined: 30 Jul 2007 Posts: 516
|
Posted: Mon Jan 19, 2009 12:20 am Post subject: Wrapper to catch Power Management events (like standby) |
|
|
I made a nice little wrapper in order to catch Power Management Events. A few examples of events:
- Detect when the computer is trying to suspend.
- Detect when the computer tried to suspend, but failed.
- Detect when the computer resumes from a suspended state.
- Detect when the battery is running low
- Detect when the computer changes power source (eg. from battery to A/C)
| Code: | /* TheGood
Wrapper to catch Power Management events
;http://msdn.microsoft.com/en-us/library/aa373162(VS.85).aspx
;SUPPORTED ONLY BY WINDOWS 2000 AND UP
WM_POWERBROADCAST := 536 ;Notifies applications that a power-management event has occurred.
PBT_APMQUERYSUSPEND := 0 ;Requests permission to suspend the computer. An application
;that grants permission should carry out preparations for the
;suspension before returning.
BROADCAST_QUERY_DENY := 1112363332 ;Return this value to deny the request.
PBT_APMQUERYSUSPENDFAILED := 2 ;Notifies applications that permission to suspend the computer
;was denied. This event is broadcast if any application or
;driver returned BROADCAST_QUERY_DENY to a previous
;PBT_APMQUERYSUSPEND event.
PBT_APMSUSPEND := 4 ;Notifies applications that the computer is about to enter a
;suspended state. This event is typically broadcast when all
;applications and installable drivers have returned TRUE to a
;previous PBT_APMQUERYSUSPEND event.
PBT_APMRESUMECRITICAL := 6 ;Notifies applications that the system has resumed operation.
;This event can indicate that some or all applications did not
;receive a PBT_APMSUSPEND event. For example, this event can be
;broadcast after a critical suspension caused by a failing
;battery.
PBT_APMRESUMESUSPEND := 7 ;Notifies applications that the system has resumed operation
;after being suspended.
PBT_APMBATTERYLOW := 9 ;Notifies applications that the battery power is low.
PBT_APMPOWERSTATUSCHANGE := 10 ;Notifies applications of a change in the power status of the
;computer, such as a switch from battery power to A/C. The
;system also broadcasts this event when remaining battery power
;slips below the threshold specified by the user or if the
;battery power changes by a specified percentage.
PBT_APMOEMEVENT := 11 ;Notifies applications that the APM BIOS has signaled an APM
;OEM event.
PBT_APMRESUMEAUTOMATIC := 18 ;Notifies applications that the computer has woken up
;automatically to handle an event. An application will not
;generally respond unless it is handling the event, because
;the user is not present.
*/
OnMessage(536, "OnPBMsg") ;WM_POWERBROADCAST
Return
OnPBMsg(wParam, lParam, msg, hwnd) {
If (wParam = 0) { ;PBT_APMQUERYSUSPEND
If (lParam & 1) ;Check action flag
MsgBox The computer is trying to suspend, and user interaction is permitted.
Else MsgBox The computer is trying to suspend, and no user interaction is allowed.
;Return TRUE to grant the request, or BROADCAST_QUERY_DENY to deny it.
} Else If (wParam = 2) ;PBT_APMQUERYSUSPENDFAILED
MsgBox The computer tried to suspend, but failed.
Else If (wParam = 4) ;PBT_APMSUSPEND
MsgBox The computer is about to enter a suspended state.
Else If (wParam = 6) ;PBT_APMRESUMECRITICAL
MsgBox The computer is now resuming from a suspended state, which may have taken place unexpectedly.
Else If (wParam = 7) ;PBT_APMRESUMESUSPEND
MsgBox The computer is now resuming from a suspended state.
Else If (wParam = 9) ;PBT_APMBATTERYLOW
MsgBox The computer battery is running low.
Else If (wParam = 10) ;PBT_APMPOWERSTATUSCHANGE
MsgBox The computer power status has changed.
Else If (wParam = 11) ;PBT_APMOEMEVENT
MsgBox The APM BIOS has signaled an APM OEM event with event code %lParam%
Else If (wParam = 18) ;PBT_APMRESUMEAUTOMATIC
MsgBox The computer is now automatically resuming from a suspended state.
;Must return True after message is processed
Return True
} |
Edit: Replaced the constants with their actual values.
Last edited by TheGood on Tue Jan 20, 2009 4:50 pm; edited 2 times in total |
|
| Back to top |
|
 |
Frankie
Joined: 02 Nov 2008 Posts: 828
|
Posted: Mon Jan 19, 2009 12:54 am Post subject: |
|
|
Nice. Havn't tried it out yet but Im planing on making a GDI+ low battery warning with this. _________________ Click here to join #AHK channel in IRC for general chat and quick help. |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 7609
|
Posted: Tue Jan 20, 2009 7:32 am Post subject: |
|
|
Nice.. Couple of suggestions.
1) It is better not to use MsgBox within Message Monitor Functions. I would suggest TrayTip.
2) The functions has to return a True as suggested by MSDN Doc. _________________ Suresh Kumar A N |
|
| Back to top |
|
 |
Zer07even
Joined: 01 Aug 2008 Posts: 19 Location: South Africa
|
Posted: Tue Jan 20, 2009 9:02 am Post subject: |
|
|
Maybe just me but absolutely nothing happens on vista. msgbox never shows _________________
 |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 7609
|
Posted: Tue Jan 20, 2009 9:28 am Post subject: |
|
|
@Zer07even
Replace
| Code: | | OnMessage(WM_POWERBROADCAST, "OnPBMsg") |
with
| Code: | | OnMessage( (WM_POWERBROADCAST:=0x218), "OnPBMsg") |
_________________ Suresh Kumar A N |
|
| Back to top |
|
 |
Zer07even
Joined: 01 Aug 2008 Posts: 19 Location: South Africa
|
Posted: Tue Jan 20, 2009 11:17 am Post subject: |
|
|
Working beautifully! I tried to write something like this a while back but i could never get it working, now I know why.
Thnx _________________
 |
|
| Back to top |
|
 |
TheGood
Joined: 30 Jul 2007 Posts: 516
|
Posted: Tue Jan 20, 2009 4:19 pm Post subject: |
|
|
| SKAN wrote: | Nice.. Couple of suggestions.
1) It is better not to use MsgBox within Message Monitor Functions. I would suggest TrayTip. |
True. I was just using MsgBox to show people where their code would go. It's not meant to be implemented that way. Maybe I should just put a comment there instead?
| SKAN wrote: |
2) The functions has to return a True as suggested by MSDN Doc. |
As far as I can tell, only PBT_APMQUERYSUSPEND requires a return value. The others simply ignore it (MSDN says "No return value."). |
|
| Back to top |
|
 |
TheGood
Joined: 30 Jul 2007 Posts: 516
|
Posted: Tue Jan 20, 2009 4:21 pm Post subject: |
|
|
| SKAN wrote: | @Zer07even
Replace
| Code: | | OnMessage(WM_POWERBROADCAST, "OnPBMsg") |
with
| Code: | | OnMessage( (WM_POWERBROADCAST:=0x218), "OnPBMsg") |
|
THanks! I forgot to change the constant for its actual value for that one
Updated post! |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 7609
|
Posted: Tue Jan 20, 2009 4:30 pm Post subject: |
|
|
| TheGood wrote: | | As far as I can tell, only PBT_APMQUERYSUSPEND requires a return value. The others simply ignore it (MSDN says "No return value."). |
| MSDN wrote: | WM_POWERBROADCAST Message
Return Value
An application should return TRUE if it processes this message.
Windows Server 2003, Windows XP, and Windows 2000: An application can return BROADCAST_QUERY_DENY to deny a PBT_APMQUERYSUSPEND or PBT_APMQUERYSUSPENDFAILED request. |
|
|
| Back to top |
|
 |
TheGood
Joined: 30 Jul 2007 Posts: 516
|
Posted: Tue Jan 20, 2009 4:51 pm Post subject: |
|
|
OK, I updated the post. Thanks!  |
|
| Back to top |
|
 |
Beery
Joined: 04 Mar 2006 Posts: 3
|
Posted: Tue Apr 06, 2010 6:49 pm Post subject: |
|
|
Very useful!
A question. I'm getting multiple triggers when plugging my laptop of of AC power - is this expected? I was expecting only one event.
Thanks,
Beery |
|
| Back to top |
|
 |
TheGood
Joined: 30 Jul 2007 Posts: 516
|
Posted: Tue Apr 06, 2010 7:25 pm Post subject: |
|
|
Not sure. I don't have a laptop to test it.
Is it the same event multiple times or different events each time? |
|
| Back to top |
|
 |
theInFan
Joined: 17 Oct 2009 Posts: 5
|
Posted: Wed Jun 30, 2010 4:50 pm Post subject: |
|
|
Its the same event.
I don't know about Beery, but when I plug into AC the power status event triggers twice.
But only one trigger when I unplug and go to battery power.
It might be because there are two states when plugging into AC power. According to Windows 7, those two states are:
"plugged in, not charging"
"plugged in, charging"
Each state would trigger the power status event, creating this "double trigger" situation. |
|
| Back to top |
|
 |
|