on plug

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Mrqsyaka
Posts: 3
Joined: 25 Aug 2021, 10:02

on plug

Post by Mrqsyaka » 25 Aug 2021, 10:45

Hi again, I want to create a script which runs when I plug my laptop charger. How exactly can I do that?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: on plug

Post by swagfag » 25 Aug 2021, 10:47

register for and monitor WM_POWERBROADCAST
Mrqsyaka
Posts: 3
Joined: 25 Aug 2021, 10:02

Re: on plug

Post by Mrqsyaka » 25 Aug 2021, 10:54

What should I register?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: on plug

Post by swagfag » 25 Aug 2021, 11:59

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
		}
	}
}
User avatar
epete
Posts: 81
Joined: 02 Dec 2020, 10:03

Re: on plug

Post by epete » 16 Sep 2021, 04:47

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?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: on plug

Post by swagfag » 16 Sep 2021, 17:24

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
		}
	}
}
User avatar
epete
Posts: 81
Joined: 02 Dec 2020, 10:03

Re: on plug

Post by epete » 16 Sep 2021, 21:37

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?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: on plug

Post by swagfag » 22 Sep 2021, 20:59

that sounds like something to write ur vendor about to. u probably have to install some drivers or something to get some communication going
User avatar
epete
Posts: 81
Joined: 02 Dec 2020, 10:03

Re: on plug

Post by epete » 15 Oct 2021, 14:58

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>
Post Reply

Return to “Ask for Help (v2)”