[Q] Trying to find a script that lowers laptop brightness on battery - can anyone help?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
HopelesslyConfused
Posts: 9
Joined: 07 Mar 2020, 22:15

[Q] Trying to find a script that lowers laptop brightness on battery - can anyone help?

07 Mar 2020, 22:23

Hey guys,

I've found a few AHK scripts that deal with brightness so hopefully this isn't possible.

Recent Windows 10 updates have removed the feature of being able to set the screen to dim when running on battery - so, for example, when you unplug, your brightness would automatically go from 100 to 50. Now when you unplug, the brightness stays the same.

There's no way to change this apart from using the baked-in 'lower screen brightness on battery' feature when you enable Power Saver Mode, which only lowers brightness by 10%.

My question: is it possible to make an AHK script where brightness drops from 100% to 50% when the charger is unplugged, and raises it again when the laptop is plugged in again?

Any help would be awesome. Thanks heaps.

- HopelesslyConfused
chaoscreater
Posts: 60
Joined: 12 Sep 2019, 21:15

Re: [Q] Trying to find a script that lowers laptop brightness on battery - can anyone help?

07 Mar 2020, 23:37

You can download TrayBlank and use hotkeys or the GUI to control the brightness.
HopelesslyConfused
Posts: 9
Joined: 07 Mar 2020, 22:15

Re: [Q] Trying to find a script that lowers laptop brightness on battery - can anyone help?

08 Mar 2020, 05:59

Hi,

I downloaded TrayBlank but it doesn't seem to be what I'm looking for. It's a tool to toggle screensavers with a hotkey, and doesn't seem to have anything to do with display brightness, or be able to detect if a laptop is plugged in or on battery.
vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Re: [Q] Trying to find a script that lowers laptop brightness on battery - can anyone help?

08 Mar 2020, 09:54

You have to either compile this script or use the AutoHotkey path and add the command line pointing the script

1.I don't know if you have this program or not(in comes with windows)but check if you have it
C:\Windows\System32\mblctr.exe
2.Choose how you want to use the script(command line or compiled)

Code: Select all

#SingleInstance,Force
#NoTrayIcon
#NoEnv

DetectHiddenWindows,On
Process,Exist,mblctr.exe
If ErrorLevel = 0
Run,C:\Windows\System32\mblctr.exe,,Hide
WinWait,Windows Mobility Center
IniRead,Status,% A_ScriptDir "\Power.ini",Status,Battery
If Status = On
{
SendMessage, 0x0422, 0,50, msctls_trackbar321
IniWrite,Off,% A_ScriptDir "\Power.ini",Status,Battery
}
Else
{
SendMessage, 0x0422, 0,10, msctls_trackbar321
IniWrite,On,% A_ScriptDir "\Power.ini",Status,Battery
}
; WinClose,Windows Mobility Center ; uncomment this if you want to close the mblctr.exe after that but it will make changing the brigtness 2 or 3 seconds longer
ExitApp
3.In the start menu type task scheduler and run it as admin and click on Task Scheduler Library
4.Action=>Create Task=>choose a name and enable "Run with highest privileges" and "Hidden"
5.In triggers click on new=>Begin the task: On an event=>Log to System=>Source to Kernel-Power=>Event ID to 105 and hit ok
6.In Actions=>New=>Action: Start a program and set the path to either the compiled script or the AutoHotKey path and the script path with quotes in arguments and hit ok
7.In conditions,uncheck the options in power and hit ok

If everything is done properly,the script will lower the brightness to 10% on battery and increase it to 50 when you plug the ac back in

The first time when windows run that task,you have to unplug the ac not plug it in because event 105 is triggered in both states but ahk writes to the power.ini the current state(assuming it is plugged and you are unplugging so it writes Battery=On in the ini)
HopelesslyConfused
Posts: 9
Joined: 07 Mar 2020, 22:15

Re: [Q] Trying to find a script that lowers laptop brightness on battery - can anyone help?

08 Mar 2020, 16:43

That is amazing! Thank you!

Works perfectly.

For anyone googling -

Windows 10 change brightness on battery
Windows 10 how to change brightness on AC
Windows 10 Power Plan Brightness Charging and Battery
HopelesslyConfused
Posts: 9
Joined: 07 Mar 2020, 22:15

Re: [Q] Trying to find a script that lowers laptop brightness on battery - can anyone help?

11 Mar 2020, 19:06

Hi there,

I'm just having a few issues getting this to run correctly at start up.

It seems like no matter what I do, when I first start the script (say, in the Startup folder in the start menu, where I have my other .ahk scripts), it does the wrong thing. For example, if I'm on battery the screen will brighten, if I'm on AC the screen brightness will lower.

Is there any way for me to get the script to consistently lower screen brightness on battery and raise screen brightness on power? Sorry if this is a dumb question, I'm just a bit confused.

Cheers
HopelesslyConfused
Posts: 9
Joined: 07 Mar 2020, 22:15

Re: [Q] Trying to find a script that lowers laptop brightness on battery - can anyone help?

12 Mar 2020, 03:48

Argh, now for some reason the script has stopped working entirely.

Nothing's being written to the .ini file and the script can't seem to determine if the AC is connected or if it's running on battery.

Any idea what's gone wrong? What conditions can I check to make sure the script works? I've still got Windows Mobility Center installed, so I don't think it's that
User avatar
DyaTactic
Posts: 221
Joined: 04 Apr 2017, 05:52

Re: [Q] Trying to find a script that lowers laptop brightness on battery - can anyone help?

12 Mar 2020, 05:05

I check for ACpower with this code:

Code: Select all

VarSetCapacity(powerstatus, 1+1+1+1+4+4)
If !DllCall("kernel32.dll\GetSystemPowerStatus", "uint", &powerstatus)
	ACPower := ""
Else {
	ACPower := ReadInteger(&powerstatus,0,1,false)
}

If !ACPower
{
	; Things to do when on battery.
	
} Else {
	; Things to do when pluggen in.
	
}
HopelesslyConfused
Posts: 9
Joined: 07 Mar 2020, 22:15

Re: [Q] Trying to find a script that lowers laptop brightness on battery - can anyone help?

12 Mar 2020, 05:10

D'oh, managed to fix it using your task scheduler instructions. Seems to all be working now!
vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Re: [Q] Trying to find a script that lowers laptop brightness on battery - can anyone help?

12 Mar 2020, 11:14

There is nothing in the script that can tell what mode is currently used
So yes,the only way to make it work properly is by using Task scheduler and the first time ever running the script should be from ac to battery

Running the script manually will also switch the brightness so only run it with the task scheduler
HopelesslyConfused
Posts: 9
Joined: 07 Mar 2020, 22:15

Re: [Q] Trying to find a script that lowers laptop brightness on battery - can anyone help?

13 Mar 2020, 21:11

DyaTactic wrote:
12 Mar 2020, 05:05
I check for ACpower with this code:

Code: Select all

VarSetCapacity(powerstatus, 1+1+1+1+4+4)
If !DllCall("kernel32.dll\GetSystemPowerStatus", "uint", &powerstatus)
	ACPower := ""
Else {
	ACPower := ReadInteger(&powerstatus,0,1,false)
}

If !ACPower
{
	; Things to do when on battery.
	
} Else {
	; Things to do when pluggen in.
	
}
Is there any way to modify the script so that it uses DyaTactic's code so the script knows when it's on battery/AC and adjusts brightness accordingly?
vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Re: [Q] Trying to find a script that lowers laptop brightness on battery - can anyone help?

14 Mar 2020, 02:41

That code require external library\script to work
If you do exactly what I posted in my instructions and the first time every running the script(task scheduler runs it,not you)is from ac to battery,then the only way it may not work(I did not test that so maybe it will work)is if you plug\unplug the power while the the laptop is off\sleeping(I don't know if windows triggers the event when you wake up\turn on the laptop when the last mode was different than the current mode)

The DyaTactic method also requires running the task with the task scheduler because what is does is just checks what is the current power mode when you run the scrips(it doesn't just run and wait for a specific event to be triggered)and then it requires a code that actually changes the brightness

The difference between my code and DyaTactic code is that with DyaTactic code it actually tells you what is the current mode while my script writes a value in a file while changing modes ac<=>battery
In both cases,you will need the task scheduler

With my script if it ever switches to wrong mode,just run the script manually and it will switch to the other mode and from then it,it will work properly
User avatar
DyaTactic
Posts: 221
Joined: 04 Apr 2017, 05:52

Re: [Q] Trying to find a script that lowers laptop brightness on battery - can anyone help?

14 Mar 2020, 05:09

HopelesslyConfused wrote:
13 Mar 2020, 21:11
Is there any way to modify the script so that it uses DyaTactic's code so the script knows when it's on battery/AC and adjusts brightness accordingly?
I would say that code would look like this:

Code: Select all


#SingleInstance,Force
#NoTrayIcon
#NoEnv

DetectHiddenWindows,On
Process,Exist,mblctr.exe
If ErrorLevel = 0
Run,C:\Windows\System32\mblctr.exe,,Hide
WinWait,Windows Mobility Center

VarSetCapacity(powerstatus, 1+1+1+1+4+4)
If !DllCall("kernel32.dll\GetSystemPowerStatus", "uint", &powerstatus) {
	ACPower := ""
} Else {
	ACPower := ReadInteger(&powerstatus,0,1,false)
}

If !ACPower
{
	; Power unplugged.
	SendMessage, 0x0422, 0,50, msctls_trackbar321
	;IniWrite,Off,% A_ScriptDir "\Power.ini",Status,Battery
}
Else
{
	; Running on AC power.
	SendMessage, 0x0422, 0,10, msctls_trackbar321
	;IniWrite,On,% A_ScriptDir "\Power.ini",Status,Battery
}
; WinClose,Windows Mobility Center ; uncomment this if you want to close the mblctr.exe after that but it will make changing the brigtness 2 or 3 seconds longer
ExitApp
vsub is right about the Task Scheduler, this script does not trigger on it's own when the power state changes. You can though use a timer which checks GetSystemPowerStatus every 10 s or so and take action when the state has changed.
Let me know if I missing something here.
HopelesslyConfused
Posts: 9
Joined: 07 Mar 2020, 22:15

Re: [Q] Trying to find a script that lowers laptop brightness on battery - can anyone help?

14 Mar 2020, 20:30

vsub wrote:
14 Mar 2020, 02:41
That code require external library\script to work
If you do exactly what I posted in my instructions and the first time every running the script(task scheduler runs it,not you)is from ac to battery,then the only way it may not work(I did not test that so maybe it will work)is if you plug\unplug the power while the the laptop is off\sleeping(I don't know if windows triggers the event when you wake up\turn on the laptop when the last mode was different than the current mode)

The DyaTactic method also requires running the task with the task scheduler because what is does is just checks what is the current power mode when you run the scrips(it doesn't just run and wait for a specific event to be triggered)and then it requires a code that actually changes the brightness

The difference between my code and DyaTactic code is that with DyaTactic code it actually tells you what is the current mode while my script writes a value in a file while changing modes ac<=>battery
In both cases,you will need the task scheduler

With my script if it ever switches to wrong mode,just run the script manually and it will switch to the other mode and from then it,it will work properly
That makes sense, thank you. I didn't understand. Thanks to you and DyaTactic, greatly appreciate your help :)
HopelesslyConfused
Posts: 9
Joined: 07 Mar 2020, 22:15

Re: [Q] Trying to find a script that lowers laptop brightness on battery - can anyone help?

04 Apr 2020, 18:21

vsub wrote:
14 Mar 2020, 02:41
With my script if it ever switches to wrong mode,just run the script manually and it will switch to the other mode and from then it,it will work properly
Hey Vsub, thanks again for your script.

I've been running it successfully for a while now, but I've restarted and have a situation where it's in the wrong mode (thinks power is battery, thinks battery is power).

This happened once before and I ran the script manually and it changed the brightness. This time, it's not working - have you got any advice?

Thanks!
vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Re: [Q] Trying to find a script that lowers laptop brightness on battery - can anyone help?

04 Apr 2020, 18:50

I am not sure why it will suddenly stop working,try it like this

Code: Select all

#SingleInstance,Force
#NoTrayIcon
#NoEnv

DetectHiddenWindows,On
Process,Exist,mblctr.exe
If ErrorLevel = 0
Run,C:\Windows\System32\mblctr.exe,,Hide
IniRead,Status,% A_ScriptDir "\Power.ini",Status,Battery
WinWait,Windows Mobility Center
If Status = On
{
SendMessage, 0x0422, 0,50, msctls_trackbar321,Windows Mobility Center
IniWrite,Off,% A_ScriptDir "\Power.ini",Status,Battery
}
Else
{
SendMessage, 0x0422, 0,10, msctls_trackbar321,Windows Mobility Center
IniWrite,On,% A_ScriptDir "\Power.ini",Status,Battery
}
; WinClose,Windows Mobility Center ; uncomment this if you want to close the mblctr.exe after that but it will make changing the brigtness 2 or 3 seconds longer
ExitApp
Does it switches modes or don't change the brightness at all?
lblb
Posts: 190
Joined: 30 Sep 2013, 11:31

Re: [Q] Trying to find a script that lowers laptop brightness on battery - can anyone help?

04 Apr 2020, 20:02

Does something like this work for you? (It might stop working if you later change powerplan or change the brightness manually.)

Code: Select all

;based on code by qwerty12:
;https://autohotkey.com/boards/viewtopic.php?f=6&t=26921
#Persistent

;note: AC is the brightness when the laptop is plugged in
vBrightnessAC = 80
vBrightnessDC = 5

DllCall("powrprof\PowerGetActiveScheme", Ptr,0, PtrP,vActivePolicyGuid, UInt)
VarSetCapacity(GUID_VIDEO_SUBGROUP, 16)
NumPut(0x7516B95F, &GUID_VIDEO_SUBGROUP, 0, "UInt"), NumPut(0x4464F776, &GUID_VIDEO_SUBGROUP, 4, "UInt")
NumPut(0x1606538C, &GUID_VIDEO_SUBGROUP, 8, "UInt"), NumPut(0x99CC407F, &GUID_VIDEO_SUBGROUP, 12, "UInt")
VarSetCapacity(GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS, 16)
NumPut(0xADED5E82, &GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS, 0, "UInt"), NumPut(0x4619B909, &GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS, 4, "UInt")
NumPut(0xD7F54999, &GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS, 8, "UInt"), NumPut(0xCB0BAC1D, &GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS, 12, "UInt")
DllCall("powrprof\PowerWriteDCValueIndex", Ptr,0, Ptr,vActivePolicyGuid, Ptr,&GUID_VIDEO_SUBGROUP, Ptr,&GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS, UInt,vBrightnessDC, UInt)
DllCall("powrprof\PowerWriteACValueIndex", Ptr,0, Ptr,vActivePolicyGuid, Ptr,&GUID_VIDEO_SUBGROUP, Ptr,&GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS, UInt,vBrightnessAC, UInt)
DllCall("powrprof\PowerSetActiveScheme", Ptr,0, Ptr,vActivePolicyGuid, UInt)
;DllCall("powrprof\PowerReadACValueIndex", Ptr,0, Ptr,vActivePolicyGuid, Ptr,&GUID_VIDEO_SUBGROUP, Ptr,&GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS, UIntP,vBrightnessAC, UInt)
;DllCall("powrprof\PowerReadDCValueIndex", Ptr,0, Ptr,vActivePolicyGuid, Ptr,&GUID_VIDEO_SUBGROUP, Ptr,&GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS, UIntP,vBrightnessDC, UInt)
Return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Fredrik F, maxkill, ShatterCoder and 397 guests