Battery life

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
toonpeters
Posts: 1
Joined: 30 Apr 2018, 03:14

Battery life

30 Apr 2018, 03:21

I have been searching for a script that shows the battery percentage if I press a shortcut. It shows it if I press for example the windows key and if I press the windowskey again it goes away.
I already found this on the internet but I don't know how to modyfy it to the shortcut, probably quite simple :D .

Code: Select all


; https://autohotkey.com/board/topic/7022-acbattery-status/

ReadInteger( p_address, p_offset, p_size, p_hex=true )
{
  value = 0
  old_FormatInteger := a_FormatInteger
  if ( p_hex )
    SetFormat, integer, hex
  else
    SetFormat, integer, dec
  loop, %p_size%
    value := value+( *( ( p_address+p_offset )+( a_Index-1 ) ) << ( 8* ( a_Index-1 ) ) )
  SetFormat, integer, %old_FormatInteger%
  return, value
}

GetBatteryStatus()
{
	VarSetCapacity(powerStatus, 1+1+1+1+4+4)
	success := DllCall("GetSystemPowerStatus", "UInt", &powerStatus)
	
	acLineStatus:=ReadInteger(&powerstatus,0,1,false)
	batteryFlag:=ReadInteger(&powerstatus,1,1,false)
	batteryLifePercent:=ReadInteger(&powerstatus,2,1,false)
	batteryLifeTime:=ReadInteger(&powerstatus,4,4,false)
	batteryFullLifeTime:=ReadInteger(&powerstatus,8,4,false)
	
	output := {}
	output.acLineStatus := acLineStatus
	output.batteryFlag := batteryFlag
	output.batteryLifePercent := batteryLifePercent
	output.batteryLifeTime := batteryLifeTime
	output.batteryFullLifeTime := batteryFullLifeTime
	
	Return output
}
#SingleInstance force
#NoTrayIcon


GUI:
	Temp := "--"
	
	Gui, Font, s14 w700, Arial
	Gui, Add, Text, w280 x10 y10 Center vGuiAcLineStatus, %Temp%
	
	Gui, Font, s48 w700, Arial
	Gui, Add, Text, w280 x10 y30 Center vGuiBatteryPercent, %Temp%`%
	
	Gui, Show, Center w300, AHK Battery Percent
	
	Loop
	{
		BatteryStatus := GetBatteryStatus()
		
		If (BatteryStatus.acLineStatus == 0)
		{
			Gui, Font, s14 w700 cRed, Arial
			Temp := "Discharging"
		}
		Else
		{
			Gui, Font,  s14 w700 cGreen, Arial
			Temp := "Charging"
		}
		
		GuiControl, Font, GuiAcLineStatus
		GuiControl, , GuiAcLineStatus, %Temp%
		
		Temp := BatteryStatus.batteryLifePercent
		If (Temp == 255)
		{
			Temp := "?"
		}
		Else
		{
			Temp = %Temp%`%
		}
		
		GuiControl, , GuiBatteryPercent, %Temp%
		
		Sleep, 1000
	}
Return

Last edited by BoBo on 16 Mar 2022, 01:33, edited 1 time in total.
Reason: Moved to 'Ask for Help'.
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: Battery life

25 Sep 2018, 09:39

Could this work as a shortcut to Win?:

Code: Select all

#::
GetBatteryStatus()

; https://autohotkey.com/board/topic/7022-acbattery-status/

ReadInteger( p_address, p_offset, p_size, p_hex=true )
{
  value = 0
  old_FormatInteger := a_FormatInteger
  if ( p_hex )
    SetFormat, integer, hex
  else
    SetFormat, integer, dec
  loop, %p_size%
    value := value+( *( ( p_address+p_offset )+( a_Index-1 ) ) << ( 8* ( a_Index-1 ) ) )
  SetFormat, integer, %old_FormatInteger%
  return, value
}

GetBatteryStatus()
{
	VarSetCapacity(powerStatus, 1+1+1+1+4+4)
	success := DllCall("GetSystemPowerStatus", "UInt", &powerStatus)
	
	acLineStatus:=ReadInteger(&powerstatus,0,1,false)
	batteryFlag:=ReadInteger(&powerstatus,1,1,false)
	batteryLifePercent:=ReadInteger(&powerstatus,2,1,false)
	batteryLifeTime:=ReadInteger(&powerstatus,4,4,false)
	batteryFullLifeTime:=ReadInteger(&powerstatus,8,4,false)
	
	output := {}
	output.acLineStatus := acLineStatus
	output.batteryFlag := batteryFlag
	output.batteryLifePercent := batteryLifePercent
	output.batteryLifeTime := batteryLifeTime
	output.batteryFullLifeTime := batteryFullLifeTime
	
	Return output
}
#SingleInstance force
#NoTrayIcon


GUI:
	Temp := "--"
	
	Gui, Font, s14 w700, Arial
	Gui, Add, Text, w280 x10 y10 Center vGuiAcLineStatus, %Temp%
	
	Gui, Font, s48 w700, Arial
	Gui, Add, Text, w280 x10 y30 Center vGuiBatteryPercent, %Temp%`%
	
	Gui, Show, Center w300, AHK Battery Percent
	
	Loop
	{
		BatteryStatus := GetBatteryStatus()
		
		If (BatteryStatus.acLineStatus == 0)
		{
			Gui, Font, s14 w700 cRed, Arial
			Temp := "Discharging"
		}
		Else
		{
			Gui, Font,  s14 w700 cGreen, Arial
			Temp := "Charging"
		}
		
		GuiControl, Font, GuiAcLineStatus
		GuiControl, , GuiAcLineStatus, %Temp%
		
		Temp := BatteryStatus.batteryLifePercent
		If (Temp == 255)
		{
			Temp := "?"
		}
		Else
		{
			Temp = %Temp%`%
		}
		
		GuiControl, , GuiBatteryPercent, %Temp%
		
		Sleep, 1000
	}
Return
joomb
Posts: 9
Joined: 08 Mar 2022, 19:33

Re: Battery life

15 Mar 2022, 22:52

carno wrote:
25 Sep 2018, 09:39
Could this work as a shortcut to Win?:

Code: Select all

#::
GetBatteryStatus()

; https://autohotkey.com/board/topic/7022-acbattery-status/

ReadInteger( p_address, p_offset, p_size, p_hex=true )
{
  value = 0
  old_FormatInteger := a_FormatInteger
  if ( p_hex )
    SetFormat, integer, hex
  else
    SetFormat, integer, dec
  loop, %p_size%
    value := value+( *( ( p_address+p_offset )+( a_Index-1 ) ) << ( 8* ( a_Index-1 ) ) )
  SetFormat, integer, %old_FormatInteger%
  return, value
}

GetBatteryStatus()
{
	VarSetCapacity(powerStatus, 1+1+1+1+4+4)
	success := DllCall("GetSystemPowerStatus", "UInt", &powerStatus)
	
	acLineStatus:=ReadInteger(&powerstatus,0,1,false)
	batteryFlag:=ReadInteger(&powerstatus,1,1,false)
	batteryLifePercent:=ReadInteger(&powerstatus,2,1,false)
	batteryLifeTime:=ReadInteger(&powerstatus,4,4,false)
	batteryFullLifeTime:=ReadInteger(&powerstatus,8,4,false)
	
	output := {}
	output.acLineStatus := acLineStatus
	output.batteryFlag := batteryFlag
	output.batteryLifePercent := batteryLifePercent
	output.batteryLifeTime := batteryLifeTime
	output.batteryFullLifeTime := batteryFullLifeTime
	
	Return output
}
#SingleInstance force
#NoTrayIcon


GUI:
	Temp := "--"
	
	Gui, Font, s14 w700, Arial
	Gui, Add, Text, w280 x10 y10 Center vGuiAcLineStatus, %Temp%
	
	Gui, Font, s48 w700, Arial
	Gui, Add, Text, w280 x10 y30 Center vGuiBatteryPercent, %Temp%`%
	
	Gui, Show, Center w300, AHK Battery Percent
	
	Loop
	{
		BatteryStatus := GetBatteryStatus()
		
		If (BatteryStatus.acLineStatus == 0)
		{
			Gui, Font, s14 w700 cRed, Arial
			Temp := "Discharging"
		}
		Else
		{
			Gui, Font,  s14 w700 cGreen, Arial
			Temp := "Charging"
		}
		
		GuiControl, Font, GuiAcLineStatus
		GuiControl, , GuiAcLineStatus, %Temp%
		
		Temp := BatteryStatus.batteryLifePercent
		If (Temp == 255)
		{
			Temp := "?"
		}
		Else
		{
			Temp = %Temp%`%
		}
		
		GuiControl, , GuiBatteryPercent, %Temp%
		
		Sleep, 1000
	}
Return
why not use numput() fuction ?
joomb
Posts: 9
Joined: 08 Mar 2022, 19:33

Re: Battery life

16 Mar 2022, 00:19

Code: Select all

		VarSetCapacity(SYSTEM_POWER_STATUS, 12, 0)
		DllCall("GetSystemPowerStatus", "UInt", &SYSTEM_POWER_STATUS)
		ACLineStatus := NumGet(SYSTEM_POWER_STATUS, 0, "UChar")
		BatteryFlag := NumGet(SYSTEM_POWER_STATUS, 1, "UChar")
		BatteryLifePercent := NumGet(SYSTEM_POWER_STATUS, 2, "UChar")
		;SystemStatusFlag := NumGet(SYSTEM_POWER_STATUS, 3, "UChar")
		BatteryLifeTime := NumGet(SYSTEM_POWER_STATUS, 4, "UInt")
		BatteryFullLifeTime := NumGet(SYSTEM_POWER_STATUS, 8, "UInt")	
here is the real simple and correct!
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Battery life

16 Mar 2022, 02:18

joomb wrote:
16 Mar 2022, 00:19
here is the real simple and correct!
Thx :thumbup:

Well, I've pushed your code back into an object :mrgreen:

Code: Select all

#SingleInstance, Force
SetBatchLines, -1

VarSetCapacity(SYSTEM_POWER_STATUS, 12, 0)
DllCall("GetSystemPowerStatus", "UInt", &SYSTEM_POWER_STATUS)
power := {"ACLineStatus":NumGet(SYSTEM_POWER_STATUS, 0, "UChar")
         ,"BatteryFlag":NumGet(SYSTEM_POWER_STATUS, 1, "UChar")
         ,"BatteryLifePercent":NumGet(SYSTEM_POWER_STATUS, 2, "UChar")
         ,"SystemStatusFlag":NumGet(SYSTEM_POWER_STATUS, 3, "UChar")
         ,"BatteryLifeTime":NumGet(SYSTEM_POWER_STATUS, 4, "UInt")
         ,"BatteryFullLifeTime":NumGet(SYSTEM_POWER_STATUS, 8, "UInt")}

MsgBox % power.BatteryLifePercent "%"
MsgBox % power.ACLineStatus
joomb
Posts: 9
Joined: 08 Mar 2022, 19:33

Re: Battery life

16 Mar 2022, 02:58

nice :dance: :dance: :dance:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww, rc76 and 346 guests