OnResume() - action when resume from sleep/standby/lock/etc

Post your working scripts, libraries and tools
gwarble
Posts: 220
Joined: 30 Sep 2013, 15:01

OnResume() - action when resume from sleep/standby/lock/etc

05 Sep 2017, 20:50

this can probably be improved upon by some of you geniuses, but here's a tidy/simple function for running a subroutine when resuming from sleep, standby or hibernate, or unlocking or logging on while running. Lots more options could be added and full wrapping of WM_PowerBroadcast and WM_WTSSession_Change would be better, but in my case this is all I need.

Use:
OnResume( Label [, Delay ] )
where
Label = Subroutine to run
Delay = delay in ms

Code: [Select all] [Expand] [Download] (OnResume.ahk)GeSHi © Codebox Plus



feedback appreciated
enjoy
- gwarble
Last edited by gwarble on 06 Sep 2017, 02:54, edited 2 times in total.
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
Helgef
Posts: 2404
Joined: 17 Jul 2016, 01:02
Contact:

Re: OnResume() - action when resume from sleep/standby/lock/etc

06 Sep 2017, 01:53

Awesome gwarble :thumbup:
I tested it when unlocking, works perfect!
I might adapt it for v2 and change it to call a function instead of label, do you mind if I post that version here, in case someone else wants it for v2?

Cheers and thanks for sharing.

Edit: RegisterSuspendResumeNotification is windows 8 or newer. v2 gives the error, not v1.


Edit:
gwarble wrote:Please do...

Ok, this was my first attempt, only tested on the example. I check for OS >= win 8, and omit the RegisterSuspendResumeNotification call if needed.
Mini docs:
  • fn, func or boundfunc to be called on any resume event. fn is passed an event array describing the event. Eg [0x2B1, 0x8] on unlock
  • delay, delay in ms before fn is called after the event
  • Msg, internal use, omit
  • hwnd, pass a negative value if you want to update fn and delay. Else omit.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Last edited by Helgef on 09 Sep 2017, 16:10, edited 2 times in total.
User avatar
jNizM
Posts: 2290
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: OnResume() - action when resume from sleep/standby/lock/etc

06 Sep 2017, 02:25

An example for WM_WTSSESSION_CHANGE (GitHub)
[AHK] 1.1.26.01 x64 Unicode | [WIN] 10 Pro (Version 1703) x64
My GitHub Profile | Donations are appreciated if I could help you
gwarble
Posts: 220
Joined: 30 Sep 2013, 15:01

Re: OnResume() - action when resume from sleep/standby/lock/etc

06 Sep 2017, 02:53

Thanks

Please do... I mainly wanted a nice streamlined function, a full wrapper for all the power stuff would be useful, but more than i need so low priority...

Function instead of label is a better idea, i'd probably implement it with a fallback of label so you could use either

I didn't notice any problem calling RegisterSuspendResumeNotification in XP or Win7, but we could make it conditional on those os's if its a problem
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
Helgef
Posts: 2404
Joined: 17 Jul 2016, 01:02
Contact:

Re: OnResume() - action when resume from sleep/standby/lock/etc

06 Sep 2017, 12:42

I updated my first post with a version for ahk v2
gwarble wrote:Please do... I mainly wanted a nice streamlined function, a full wrapper for all the power stuff would be useful, but more than i need so low priority...

We are on the same page here. If it was just a little bit more required coding, I'd do it another way do. I find this sort of dual-purpose functions both repulsive and fun to make :lol:
gwarble wrote:I didn't notice any problem calling RegisterSuspendResumeNotification in XP or Win7, but we could make it conditional on those os's if its a problem

AHK v1 will happily silently fail the dllcall, if you check errorlevel, you will find it to be -4,
DllCall wrote:-4: The specified function could not be found inside the DLL.

AHK v2, will conveniently throw an error. :thumbup:

Cheers.
iPhilip
Posts: 215
Joined: 02 Oct 2013, 12:21

Re: OnResume() - action when resume from sleep/standby/lock/etc

09 Sep 2017, 13:37

For those using AHK v1 and wanting to use a function instead of a label, I modified Helgef's version as follows:

Code: [Select all] [Expand] [Download] (OnResume v1.1.ahk)GeSHi © Codebox Plus

Cheers!
Last edited by iPhilip on 09 Sep 2017, 19:37, edited 1 time in total.
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
Helgef
Posts: 2404
Joined: 17 Jul 2016, 01:02
Contact:

Re: OnResume() - action when resume from sleep/standby/lock/etc

09 Sep 2017, 16:07

@iPhilip, cheers! :wave: :thumbup:
Just note, A_OSVersion has changed in v2, v1 gives a string like win_7, while v2 gives major.minor.build.
iPhilip wrote:

Code: [Select all] [Download] GeSHi © Codebox Plus

unlocked(t, event){
static eventNames:={0x218:{"":"WM_POWERBROADCAST",0x7:"PBT_APMRESUMESUSPEND",0x8:"PBT_APMRESUMESTANDBY",0x18:"PBT_APMRESUMEAUTOMATIC"}
, 0x2B1:{"":"WM_WTSSESSION_CHANGE",0x5:"LOGON",0x8:"UNLOCK"}}
msgbox % "Unlocked after " ((A_TickCount-t) / 1000) "s.`nEvent: " eventNames[event.1][""] " : " eventNames[event.1][event.2] "."
}
I'll take it! :)
iPhilip
Posts: 215
Joined: 02 Oct 2013, 12:21

Re: OnResume() - action when resume from sleep/standby/lock/etc

09 Sep 2017, 19:42

Hi Helgef,

Thank you for catching that! I updated the above post with a function from jNizM that gives the OS version as major.minor.build. :)
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Google [Bot] and 10 guests