AutoHotkey Community

It is currently May 26th, 2012, 2:24 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: January 19th, 2009, 12:20 am 
Offline

Joined: July 30th, 2007, 11:32 pm
Posts: 581
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 January 20th, 2009, 4:50 pm, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 19th, 2009, 12:54 am 
Offline
User avatar

Joined: November 2nd, 2008, 4:23 pm
Posts: 2906
Location: 127.0.0.1
Nice. Havn't tried it out yet but Im planing on making a GDI+ low battery warning with this.

_________________
aboutscriptappsscripts
Any code ⇈ above ⇈ requires AutoHotkey_L to run


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 20th, 2009, 7:32 am 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
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.

_________________
URLGet - Internet Explorer based Downloader
StartEx - Portable Shortcut Link


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 20th, 2009, 9:02 am 
Offline

Joined: August 1st, 2008, 8:50 am
Posts: 19
Location: South Africa
Maybe just me but absolutely nothing happens on vista. msgbox never shows

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 20th, 2009, 9:28 am 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
@Zer07even

Replace
Code:
OnMessage(WM_POWERBROADCAST, "OnPBMsg")

with
Code:
OnMessage( (WM_POWERBROADCAST:=0x218), "OnPBMsg")

_________________
URLGet - Internet Explorer based Downloader
StartEx - Portable Shortcut Link


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 20th, 2009, 11:17 am 
Offline

Joined: August 1st, 2008, 8:50 am
Posts: 19
Location: South Africa
Working beautifully! I tried to write something like this a while back but i could never get it working, now I know why.

Thnx

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 20th, 2009, 4:19 pm 
Offline

Joined: July 30th, 2007, 11:32 pm
Posts: 581
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.").


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 20th, 2009, 4:21 pm 
Offline

Joined: July 30th, 2007, 11:32 pm
Posts: 581
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!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 20th, 2009, 4:30 pm 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 20th, 2009, 4:51 pm 
Offline

Joined: July 30th, 2007, 11:32 pm
Posts: 581
OK, I updated the post. Thanks! :D


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 6th, 2010, 6:49 pm 
Offline

Joined: March 4th, 2006, 6:25 pm
Posts: 6
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 6th, 2010, 7:25 pm 
Offline

Joined: July 30th, 2007, 11:32 pm
Posts: 581
Not sure. I don't have a laptop to test it.
Is it the same event multiple times or different events each time?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 30th, 2010, 4:50 pm 
Offline

Joined: October 17th, 2009, 2:05 am
Posts: 5
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.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: April 15th, 2012, 5:53 pm 
Offline

Joined: March 4th, 2006, 6:25 pm
Posts: 6
Hi,

I've switched to a Windows 7 machine and (at least) the standby and resume do not trigger (PBT_APMSUSPEND and PBT_APMRESUMESUSPEND).

btw, PBT_APMPOWERSTATUSCHANGE seems to work.

Any idea?

Thanks,
Beery


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google Feedfetcher, SKAN and 12 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group