AutoHotkey Community

It is currently May 25th, 2012, 1:44 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 24 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: August 2nd, 2007, 10:07 am 
Offline

Joined: January 30th, 2005, 11:18 pm
Posts: 133
Location: Darmstadt, Germany
Hi folks,

this little script can act as an example for doing something when the computer is sent into sleep (standby and hibernation) and also when it resumes from that state.

Have fun,
Rob

Code:
/*
   Mute on hibernation
   
   Purpose:
      Mute the internal speakers if the computer is sent into hibernation or standby mode.
      
      Some laptop models of Fujitsu-Siemens are making short beep sounds everytime
      they are sent into sleep mode. This annoying behavior cannot be switched off,
      so this program circumvents it by disabling the speakers before sleep mode and
      reactivating them when resuming from hibernation.
   
   Functioning:
      The program runs in background, listening for the system´s message to hibernate
      the computer. It then mutes the speakers and turns them on again after the
      computer woke up.
      The mute state is left untouched in case the speakers were not muted by this
      program.
      The program will create one registry entry to save the mute state.
   
   Requirements:
      - Windows 2000 and newer
      - Read and write permission to HKEY_CURRENT_USER registry stem.
      
   
   Author:  Dirk Schwarzmann
   Version: 0.2
   Date:    2007-08-02
   
   Contact:
      Dirk 'Rob' Schwarzmann
      http://www.dirk-schwarzmann.de
      mailto://dirk@dirk-schwarzmann.de
   
   Version history:
      0.1, 2007-08-01: Initial version
      0.2, 2007-08-02: Replace unreliable timer function by real power event parameters
*/

; Registry location to save the mute state
reg_root = HKEY_CURRENT_USER
reg_path = SessionInformation
reg_key  = MutedforSuspend

; Tray icon and menu definition
Menu, TRAY, Icon, MuteOnHibernate.ico, 1, 1
Menu, TRAY, Tip, Mute speakers on hibernation

; Listen to the Windows power event "WM_POWERBROADCAST" (ID: 0x218):
OnMessage(0x218, "func_WM_POWERBROADCAST")
Return

/*
   This function is executed if the system sends a power event.
   Parameters wParam and lParam define the type of event:
   
   lParam: always 0
   wParam:
      PBT_APMQUERYSUSPEND             0x0000
      PBT_APMQUERYSTANDBY             0x0001
      
      PBT_APMQUERYSUSPENDFAILED       0x0002
      PBT_APMQUERYSTANDBYFAILED       0x0003
      
      PBT_APMSUSPEND                  0x0004
      PBT_APMSTANDBY                  0x0005
      
      PBT_APMRESUMECRITICAL           0x0006
      PBT_APMRESUMESUSPEND            0x0007
      PBT_APMRESUMESTANDBY            0x0008
      
      PBTF_APMRESUMEFROMFAILURE       0x00000001
      
      PBT_APMBATTERYLOW               0x0009
      PBT_APMPOWERSTATUSCHANGE        0x000A
      
      PBT_APMOEMEVENT                 0x000B
      PBT_APMRESUMEAUTOMATIC          0x0012
      
      Source: http://weblogs.asp.net/ralfw/archive/2003/09/09/26908.aspx
*/
func_WM_POWERBROADCAST(wParam, lParam)
{
   Global reg_root, reg_path, reg_key
   
   If (lParam = 0) {
      ; PBT_APMSUSPEND or PBT_APMSTANDBY? -> System will sleep
      If (wParam = 4 OR wParam = 5) {
         ; Get mute state
         SoundGet, muteState, MASTER, MUTE, 1
         
         ; If sound was not already muted, we have to do it. Otherwise the user
         ; himself had muted the speakers so we must not do anything.
         If (muteState = "Off") {
            ; Mute the speakers...
            SoundSet, 1, MASTER, MUTE, 1
            ; ...and save the fact that WE have done that. This is necessary to
            ; reset the previous state after resuming from suspend mode.
            RegWrite, REG_DWORD, %reg_root%, %reg_path%, %reg_key%, 1
         }
      }
      
      ; PBT_APMRESUMESUSPEND oder PBT_APMRESUMESTANDBY? -> System wakes up
      If (wParam = 7 OR wParam = 8) {
         ; Did WE mute the speakers before?
         RegRead, muteState, %reg_root%, %reg_path%, %reg_key%
         
         ; If yes (muteState is 1), we have to switch on the speakers.
         ; Otherwise we do nothing because it was the user who had muted the speakers.
         If (muteState = "1") {
            RegWrite, REG_DWORD, %reg_root%, %reg_path%, %reg_key%, 0
            SoundSet, 0, MASTER, MUTE, 1
         }
      }
   }
   Return
}


Last edited by RobOtter on August 2nd, 2007, 1:10 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 2nd, 2007, 12:43 pm 
Very cool,
detecting standby, hibernation and resuming can be very helpful.

Maybe you change the title of this topic,
to indicate that your script is not mainly about muting
but has a general usage.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 2nd, 2007, 1:13 pm 
Offline

Joined: January 30th, 2005, 11:18 pm
Posts: 133
Location: Darmstadt, Germany
[x] Done

Is this better? I guess it´s more general now...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 2nd, 2007, 1:55 pm 
Yes, indeed ;-)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: April 10th, 2008, 12:27 pm 
Offline

Joined: February 17th, 2008, 3:27 am
Posts: 24
Location: CHINA
Quote:
If (lParam = 0)


Without that you can also work!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 11th, 2008, 9:19 pm 
Offline

Joined: May 1st, 2007, 8:36 pm
Posts: 83
Location: The Netherlands
Thankyou very much! I recently needed this and only found a solution that worked, but not very good.
Edit: Actually, I also found this WM_ message but I did not know the values of wParam when the pc returns from suspend/hibernation/etc. Thanks for the information! :D

_________________
Printing css/html-formatted text


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 12th, 2008, 6:23 am 
Offline

Joined: April 12th, 2008, 5:52 am
Posts: 3
Location: england
hi friend,
how you doing?


Report this post
Top
 Profile  
Reply with quote  
 Post subject: From Mr Jerry Williams.
PostPosted: April 12th, 2008, 6:26 am 
Offline

Joined: April 12th, 2008, 5:52 am
Posts: 3
Location: england
yeh will like you to get back to me so that we can move onto it
Mr Jerry.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 12th, 2008, 6:29 am 
Offline

Joined: April 12th, 2008, 5:52 am
Posts: 3
Location: england
am fine and you?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 25th, 2008, 2:33 pm 
Offline

Joined: November 26th, 2004, 8:31 am
Posts: 21
Hmm, I have a problem with this. I wrote pretty much the same code before I even saw this post, but mine does not work at all. I only wanted to see if I am getting the messages, that's why the code only writes to a file:
Code:
OnMessage(0x218, "WM_POWERBROADCAST")
Return

WM_POWERBROADCAST(wParam, lParam)
{
   ; PBT_APMQUERYSUSPEND = 0
   ; PBT_APMRESUMESUSPEND = 7
   FileAppend, "powerbroadcast", Z:\Temp\pokus
   If (wParam == 0) {
      FileAppend, "suspend", Z:\Temp\pokus
      Return true
   } Else If (wParam = 7) {
      FileAppend, "resume", Z:\Temp\pokus
   }
}

For some reason, I don't get any output, which I take to mean that either WM_POWERBROADCAST never gets called or that my script doesn't catch it.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 25th, 2008, 5:11 pm 
Offline

Joined: May 1st, 2007, 8:36 pm
Posts: 83
Location: The Netherlands
I changed the path to my own tmp-directory and it works on my computer. I think the WM-part works, but you have to specify a file name instead of a folder for FileAppend.
pepak wrote:
Code:
FileAppend, "powerbroadcast", Z:\Temp\pokus.txt

_________________
Printing css/html-formatted text


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 26th, 2008, 2:54 am 
Offline

Joined: February 11th, 2008, 6:24 pm
Posts: 15
Location: USA
thank you! i had also just been looking for someway to detect returning from hibernation, as i never shut down my computer, and this looks like it could be helpful :)


Thanks!

nice and simple too :D

_________________
[repkam09]
AHK Beginner :P


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 14th, 2009, 3:29 am 
cool script. helped me a lot. thanks!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2009, 4:56 pm 
Offline

Joined: December 5th, 2008, 10:09 am
Posts: 1
super awesome thanks!


Report this post
Top
 Profile  
Reply with quote  
PostPosted: August 13th, 2009, 4:17 pm 
Offline

Joined: June 20th, 2009, 10:03 pm
Posts: 13
I'm not having luck with this. Can you do something like a MsgBox in the handler to tell if it is excecuting?

I tried to do the following:
Code:
OnMessage(0x218, "WM_POWERBROADCAST")
Return

WM_POWERBROADCAST(wParam, lParam)
{
  MsgBox Got a Windows Power Event: %wParam%
  RunWait taskkill /f /im vpngui.exe
}


But I never see the message box and the vpngui task is still running when I return from hibernation.

Is there some type of step where AHK needs to register with windows to get the message? (I'm running on XP)

Thanks!
--jp


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 24 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: DataLife and 24 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