my script doesn't work sometimes. Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
hancre
Posts: 248
Joined: 02 Jul 2021, 20:51

my script doesn't work sometimes.

01 Aug 2021, 04:17

This script shows how much battery remains in notebook.
It works well or doesn't work sometimes. How can I fix the error? Thanks for any help.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Code: Select all

^F5:: Reload
#NoTrayIcon
#Persistent
#SingleInstance, Force

#b::
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

​

}

​

;set the escape key to clear GUI window

GuiEscape: ; Note: single colon here, not double

Gui, hide

Return
[Mod edit: [code][/code] tags added.]
Last edited by gregster on 01 Aug 2021, 04:28, edited 1 time in total.
Reason: Please use [code] tags. Thank you!
User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: my script doesn't work sometimes.  Topic is solved

01 Aug 2021, 10:49

Here is mine.

Code: Select all

batt := batt()
MsgBox, 64, Battery status, % batt.2 "% remaining`n`n" batt.4 " (" batt.3 ")"

batt() {
 Static bText := ["Discharging", "AC", "Fully charged", "Low", "Critical", "Charging", "High, charging"
                , "Low, charging", "Critical, charging", "Undefined", "Partially charged"]
 val := []
 For objItem in ComObjGet("winmgmts:").ExecQuery("SELECT * FROM Win32_Battery")
  For each, item in ["BatteryStatus", "EstimatedChargeRemaining", "Status"]
   val.Push(objItem[item]) ; 1 = battery status (number), 2 = percentage remaining, 3 = status (e.g., OK)
 val.Push(bText[val.1])    ; 4 = battery status (text)
 Return val
}
hancre
Posts: 248
Joined: 02 Jul 2021, 20:51

Re: my script doesn't work sometimes.

01 Aug 2021, 22:16

mikeyww wrote:
It looks clear and works well.
Thanks for your help again. ^^

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, Google [Bot], Haris00911, RussF and 306 guests