Awesome
gwarble
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
; Note, the hotkey #l locks the computer.
; Example, show time locked when unlocking computer.
#l::onResume(func("unlocked").bind(A_TickCount),1500,,-1)
unlocked(t, event){ ; iPhilip, https://autohotkey.com/boards/viewtopic.php?p=169634#p169634
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 " round(((A_TickCount-t) / 1000),2) "s.`nEvent: " eventNames[event.1][""] " : " eventNames[event.1][event.2] ".")
}
OnResume(fn,Delay:=200,Msg:="",hwnd:=0) {
; Specify hwnd < 0 to change the notification function. (not needed on first call)
static Init, Func_, Delay_
local tf
If !Init { ;first time called
if regexreplace(A_OSVersion, "(\d\.\d).*","$1") >= 6.2
DllCall("RegisterSuspendResumeNotification", "ptr", A_ScriptHwnd, "uint", 0)
DllCall("Wtsapi32.dll\WTSRegisterSessionNotification", "uint", A_ScriptHwnd, "uint", 1 )
OnMessage(0x218, "OnResume") ;WM_POWERBROADCAST
OnMessage(0x2B1, "OnResume") ;WM_WTSSESSION_CHANGE
}
if !Init || hwnd<0 {
If type(fn) != "func" && type(fn) != "BoundFunc"
Return msgbox("invalid function")
Func_ := fn
Delay_ := Delay
Init := 1
return
}
If fn==1
return Func_.call(msg)
If (Msg = 0x218)
If (fn=0x7) || (fn=0x8) || (fn=0x18) ; PBT_APMRESUMESUSPEND=0x7, PBT_APMRESUMESTANDBY=0x8, PBT_APMRESUMEAUTOMATIC=0x18
tf:=func("OnResume").bind(1,0,[msg, fn]), SetTimer(tf, -Delay_)
If (Msg = 0x2B1)
If (fn=0x8) || (fn=0x5) ; UNLOCK=0x8,LOGON=0x5
tf:=func("OnResume").bind(1,0,[msg, fn]), SetTimer(tf, -Delay_)
}