on plug
on plug
Hi again, I want to create a script which runs when I plug my laptop charger. How exactly can I do that?
Re: on plug
register for and monitor WM_POWERBROADCAST
Re: on plug
What should I register?
Re: on plug
Code: Select all
#Requires AutoHotkey v2.0-beta.1
#SingleInstance Force
Persistent()
NumPut('Int64', 0x4B00E9D55D3E9A59, 'Int64', 0x486551FF34FFBDA6, GUID_ACDC_POWER_SOURCE := Buffer(16))
if !hPowerNotify := DllCall('RegisterPowerSettingNotification', 'Ptr', A_ScriptHwnd, 'Ptr', GUID_ACDC_POWER_SOURCE, 'UInt', 0, 'Ptr')
throw OSError(A_LastError, -1, 'RegisterPowerSettingNotification')
Sleep(1) ; a notification is always emitted immediately after registering for it for some reason. this prevents seeing it
OnMessage(0x218, WM_POWERBROADCAST)
WM_POWERBROADCAST(wParam, lParam, msg, hwnd) {
switch wParam
{
case 0x8013: ; PBT_POWERSETTINGCHANGE
switch NumGet(lParam, 20, 'UChar') ; POWERBROADCAST_SETTING.Data
{
case 0: ; charger plugged
; do what u want
case 1: ; on battery
case 2: ; on UPS
}
}
}
Re: on plug
Is there a way to use this in the current version of AutoHotKey, without requiring me to convert my whole application to v2.0-beta.1?
Re: on plug
Code: Select all
#Requires AutoHotkey v1.1.33+
#SingleInstance Force
#Persistent
VarSetCapacity(GUID_ACDC_POWER_SOURCE, 16)
NumPut(0x486551FF34FFBDA6, NumPut(0x4B00E9D55D3E9A59, GUID_ACDC_POWER_SOURCE, "Int64"), "Int64")
if !hPowerNotify := DllCall("RegisterPowerSettingNotification", "Ptr", A_ScriptHwnd, "Ptr", &GUID_ACDC_POWER_SOURCE, "UInt", 0, "Ptr")
throw Exception(A_LastError, -1, "RegisterPowerSettingNotification")
Sleep 1 ; a notification is always emitted immediately after registering for it for some reason. this prevents seeing it
OnMessage(0x218, "WM_POWERBROADCAST")
WM_POWERBROADCAST(wParam, lParam, msg, hwnd) {
switch wParam
{
case 0x8013: ; PBT_POWERSETTINGCHANGE
switch NumGet(lParam+20, "UChar") ; POWERBROADCAST_SETTING.Data
{
case 0: ; charger plugged
; do what u want
case 1: ; on battery
case 2: ; on UPS
}
}
}
Re: on plug
Thanks!!
Unfortunately my APC UPS system (model BX1000M-LM60) does not seem to issue this event when switching to UPS battery power - which seems really odd that it would not. Has anyone worked with APC UPS systems and gotten an event to happen when it switches to battery backup?
Unfortunately my APC UPS system (model BX1000M-LM60) does not seem to issue this event when switching to UPS battery power - which seems really odd that it would not. Has anyone worked with APC UPS systems and gotten an event to happen when it switches to battery backup?
Re: on plug
that sounds like something to write ur vendor about to. u probably have to install some drivers or something to get some communication going
Re: on plug
For my TrippLite UPS (I switched from APC UPS's since they didn't have what I needed) I discovered it will log an event in the Windows Application event log, if it goes on battery. The Event ID is 202. Is there a way for me to "catch" that event when it happens and act upon it? i.e., shut down my application gracefully. I don't want to repeatedly read the event log as that would slow things down too much. Just need a hook to capture the event when it happens. If it helps, this is the XML of the event that happens.
Code: Select all
- <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
- <System>
<Provider Name="PowerAlert Agent" />
<EventID Qualifiers="0">202</EventID>
<Version>0</Version>
<Level>3</Level>
<Task>0</Task>
<Opcode>0</Opcode>
<Keywords>0x80000000000000</Keywords>
<TimeCreated SystemTime="2021-10-07T18:25:49.8409462Z" />
<EventRecordID>140344</EventRecordID>
<Correlation />
<Execution ProcessID="0" ThreadID="0" />
<Channel>Application</Channel>
<Computer>DESKTOP-F63S7HN</Computer>
<Security />
</System>
- <EventData>
<Data>Device 1: On Battery</Data>
</EventData>
</Event>