Page 1 of 1

Basic Incremental Laptop Brightness Up/Down

Posted: 18 Nov 2020, 00:17
by FailSafeNow
I've tried Googling this, but everything I find seems to be a complex script that doesn't actually tell me how to do what I want. All I want is a basic function to turn my laptop brightness up or down incrementally, the same as I can do with volume via Volume_Up/Volume_Down. That's all I'm asking for.

I've got an old Lenovo laptop, X131e. It used to be a Chromebook. The row of F keys was mapped to a specific set of controls. When I installed Windows 10, the keys went back to being standard F keys, and they don't even have the F key labels. I'm just trying to turn the keys, at least most of them, back into what they were. So far I've got F1-3 remapped to Browser Back, Forward, and Refresh, and F8-10 remapped to Volume Mute, Down, and Up. Now, F6 and 7 used to be incremental brightness controls. Press the Brightness Down key, things got dimmer. Press it again, they got dimmer still. Press it enough times, the screen would be completely dark. Press Brightness Up, it got brighter. Press it again, it got even brighter. Etc., all the way to a maximum brightness. Basic stuff.

Like I say, I try to Google the scripts for these simple operations, and I'm just not finding them. I'm finding a bunch of complex scripts that seem to be designed to dim the monitor to a very specific level, or other complex scripts that don't tell me whether they're turning the brightness up or down or what. All I want is incremental up and down. How do I do that? What's the script for Up, what's the script for Down?

Thank you for any help.

Re: Basic Incremental Laptop Brightness Up/Down

Posted: 18 Nov 2020, 06:46
by mikeyww
You're right, a boatload of pages, discussions, and scripts are available for this. Below are some.

https://autohotkey.com/board/topic/121999-screen-dimming-via-autohotkey/
https://autohotkey.com/board/topic/83100-laptop-screen-brightness/
https://gist.github.com/krrr/3c3f1747480189dbb71f
https://github.com/hasantr/WinM
https://github.com/tigerlily-dev/tigerlilys-Screen-Dimmer/
https://superuser.com/questions/651706/map-fn-home-to-screen-brightness-using-autohotkey
https://www.autohotkey.com/boards/viewtopic.php?f=83&t=80086
https://www.autohotkey.com/boards/viewtopic.php?p=317763
https://www.autohotkey.com/boards/viewtopic.php?style=1&t=65129&p=279720
https://www.autohotkey.com/boards/viewtopic.php?t=26921&p=126135

These pages describe many issues, including how to use a one-liner (e.g., with NirCmd, which already handles this in a single command), and why trying to use Fn usually will not work (but you could assign a regular F function key).

Some of the scripts cited above are already compiled or can be plugged in as a function and then called in a single line, so you could use a hotkey with it.

Re: Basic Incremental Laptop Brightness Up/Down

Posted: 18 Nov 2020, 12:38
by FailSafeNow
Thank you. I finally managed to get results with the script by querty12. (The other options were no good.)

Re: Basic Incremental Laptop Brightness Up/Down

Posted: 18 Nov 2020, 15:22
by TLM
Here's a pretty simple approach using AHk + WMI

Code: Select all

; Variables
Increments 			:= 10 ; < lower for a more granular change, higher for larger jump in brightness 
CurrentBrightness 	:= GetCurrentBrightNess()

; Hot Keys
F6::ChangeBrightness( CurrentBrightness -= Increments ) ; decrease brightness
F7::ChangeBrightness( CurrentBrightness += Increments ) ; increase brightness

; Functions
ChangeBrightness( ByRef brightness, timeout = 1 )
{
	if ( brightness > 0 && brightness < 100 )
	{
		For property in ComObjGet( "winmgmts:\\.\root\WMI" ).ExecQuery( "SELECT * FROM WmiMonitorBrightnessMethods" )
			property.WmiSetBrightness( timeout, brightness )	
	}
 	else if ( brightness >= 100 )
 	{
 		brightness := 100
 	}
 	else if ( brightness <= 0 )
 	{
 		brightness := 0
 	}
}

GetCurrentBrightNess()
{
	For property in ComObjGet( "winmgmts:\\.\root\WMI" ).ExecQuery( "SELECT * FROM WmiMonitorBrightness" )
		currentBrightness := property.CurrentBrightness	

	return currentBrightness
}
I'm sure I had an even easier, less redundant approach before...
If I find it i'll post it...

Anyway hope this helps

Re: Basic Incremental Laptop Brightness Up/Down

Posted: 21 Apr 2021, 20:41
by chrismaliszewski
Great work TLM! A bit corrected code of yours that actually reaches 0 and 100 and resets the brightness.

Code: Select all

; Variables
Increments 			:= 10 ; < lower for a more granular change, higher for larger jump in brightness 
CurrentBrightness 	:= GetCurrentBrightNess()

; Hot Keys
; Win + Numpad 4/5/6
#Numpad4::     ChangeBrightness( CurrentBrightness -= Increments ) ; decrease brightness
#Numpad5::     ChangeBrightness( CurrentBrightness := 50 ) ; default
#Numpad6::     ChangeBrightness( CurrentBrightness += Increments ) ; increase brightness

; Functions
ChangeBrightness( ByRef brightness := 50, timeout = 1 )
{
	if ( brightness >= 0 && brightness <= 100 )
	{
		For property in ComObjGet( "winmgmts:\\.\root\WMI" ).ExecQuery( "SELECT * FROM WmiMonitorBrightnessMethods" )
			property.WmiSetBrightness( timeout, brightness )	
	}
 	else if ( brightness > 100 )
 	{
 		brightness := 100
 	}
 	else if ( brightness < 0 )
 	{
 		brightness := 0
 	}
}

GetCurrentBrightNess()
{
	For property in ComObjGet( "winmgmts:\\.\root\WMI" ).ExecQuery( "SELECT * FROM WmiMonitorBrightness" )
		currentBrightness := property.CurrentBrightness	

	return currentBrightness
}

Re: Basic Incremental Laptop Brightness Up/Down

Posted: 04 Aug 2021, 07:42
by submeg
chrismaliszewski wrote:
21 Apr 2021, 20:41
Great work TLM! A bit corrected code of yours that actually reaches 0 and 100 and resets the brightness.
Thanks @TLM and @chrismaliszewski! I've been wanting to implement this ability for months now and finally a search of the forums brought me to this post. Another thing to cross off the list!

Re: Basic Incremental Laptop Brightness Up/Down

Posted: 13 Aug 2021, 20:34
by LupusWelshie
chrismaliszewski wrote:
21 Apr 2021, 20:41
Great work TLM! A bit corrected code of yours that actually reaches 0 and 100 and resets the brightness.

Code: Select all

; Hot Keys
; Win + Numpad 4/5/6/2
#Numpad6::     ChangeBrightness( CurrentBrightness -= Increments ) ; decrease brightness
#Numpad5::     ChangeBrightness( CurrentBrightness := 50 ) ; default
#Numpad2::     ChangeBrightness( CurrentBrightness := 100 ) ; default
#Numpad4::     ChangeBrightness( CurrentBrightness += Increments ) ; increase brightness
(Just replace the 'Hot Keys' chunk with this and it should work ^^)

Added a bit that can snap it to 100%. After staying on 50% while messing with this, then going to 100% brightness- my eyes seared out of my skull. lol

Re: Basic Incremental Laptop Brightness Up/Down

Posted: 05 Mar 2022, 22:08
by PepeLapiu
Hey guys. I tried adding the following code to my script:

Code: Select all

; Variables
Increments 			:= 10 ; < lower for a more granular change, higher for larger jump in brightness 
CurrentBrightness 	:= GetCurrentBrightNess()

; Hot Keys
; Win + Numpad 4/5/6
#i::     ChangeBrightness( CurrentBrightness -= Increments ) ; decrease brightness
#y::     ChangeBrightness( CurrentBrightness := 50 ) ; default
#t::     ChangeBrightness( CurrentBrightness += Increments ) ; increase brightness

; Functions
ChangeBrightness( ByRef brightness := 50, timeout = 1 )
{
	if ( brightness >= 0 && brightness <= 100 )
	{
		For property in ComObjGet( "winmgmts:\\.\root\WMI" ).ExecQuery( "SELECT * FROM WmiMonitorBrightnessMethods" )
			property.WmiSetBrightness( timeout, brightness )	
	}
 	else if ( brightness > 100 )
 	{
 		brightness := 100
 	}
 	else if ( brightness < 0 )
 	{
 		brightness := 0
 	}
}

GetCurrentBrightNess()
{
	For property in ComObjGet( "winmgmts:\\.\root\WMI" ).ExecQuery( "SELECT * FROM WmiMonitorBrightness" )
		currentBrightness := property.CurrentBrightness	

	return currentBrightness
}
Excuse my noob question but I don't get it. I want the script to turn the brightness all the way down when it starts. That's all I need it for. How do I write the code to do that? I thought a simple Send,{#i} in a loop repeated 10 times would do the trick. But it's not doing anything.

Re: Basic Incremental Laptop Brightness Up/Down

Posted: 05 Mar 2022, 22:12
by boiler
Just tell it to change it directly to the brightness you want. Don't use one of the incremental hotkeys.

Code: Select all

ChangeBrightness(0)

Re: Basic Incremental Laptop Brightness Up/Down

Posted: 05 Mar 2022, 22:14
by PepeLapiu
boiler wrote:
05 Mar 2022, 22:12
Just tell it to change it directly to the brightness you want. Don't use one of the incremental hotkeys.

Code: Select all

ChangeBrightness(0)
Thanx boiler. But it's not doing anything.

Re: Basic Incremental Laptop Brightness Up/Down

Posted: 05 Mar 2022, 22:21
by PepeLapiu
Here is my test script exactly as I have it.

Code: Select all

#singleinstance, force

ChangeBrightness(0)

MsgBox, a


; Variables
Increments 			:= 10 ; < lower for a more granular change, higher for larger jump in brightness 
CurrentBrightness 	:= GetCurrentBrightNess()

; Hot Keys
h::ChangeBrightness( CurrentBrightness -= Increments ) ; decrease brightness
j::ChangeBrightness( CurrentBrightness += Increments ) ; increase brightness

; Functions
ChangeBrightness( ByRef brightness, timeout = 1 )
{
	if ( brightness > 0 && brightness < 100 )
	{
		For property in ComObjGet( "winmgmts:\\.\root\WMI" ).ExecQuery( "SELECT * FROM WmiMonitorBrightnessMethods" )
			property.WmiSetBrightness( timeout, brightness )	
	}
 	else if ( brightness >= 100 )
 	{
 		brightness := 100
 	}
 	else if ( brightness <= 0 )
 	{
 		brightness := 0
 	}
}

GetCurrentBrightNess()
{
	For property in ComObjGet( "winmgmts:\\.\root\WMI" ).ExecQuery( "SELECT * FROM WmiMonitorBrightness" )
		currentBrightness := property.CurrentBrightness	

	return currentBrightness
}
ChangeBrightness(0)
The hotkeys work. Brightness goes up and down when I click h or j. But ChangeBrightness(0) doesn't do anything

Re: Basic Incremental Laptop Brightness Up/Down

Posted: 05 Mar 2022, 22:25
by boiler
It should. Run this script and it should change it to three brightness levels:

Code: Select all

ChangeBrightness(0)
Sleep, 2000
ChangeBrightness(100)
Sleep, 2000
ChangeBrightness(50)
return

ChangeBrightness( ByRef brightness := 50, timeout = 1 )
{
	if ( brightness >= 0 && brightness <= 100 )
	{
		For property in ComObjGet( "winmgmts:\\.\root\WMI" ).ExecQuery( "SELECT * FROM WmiMonitorBrightnessMethods" )
			property.WmiSetBrightness( timeout, brightness )	
	}
 	else if ( brightness > 100 )
 	{
 		brightness := 100
 	}
 	else if ( brightness < 0 )
 	{
 		brightness := 0
 	}
}

Re: Basic Incremental Laptop Brightness Up/Down

Posted: 05 Mar 2022, 22:28
by PepeLapiu
boiler wrote:
05 Mar 2022, 22:25
It should. Run this script and it should change it to three brightness levels:

Code: Select all

ChangeBrightness(0)
Sleep, 2000
ChangeBrightness(100)
Sleep, 2000
ChangeBrightness(50)
return

ChangeBrightness( ByRef brightness := 50, timeout = 1 )
{
	if ( brightness >= 0 && brightness <= 100 )
	{
		For property in ComObjGet( "winmgmts:\\.\root\WMI" ).ExecQuery( "SELECT * FROM WmiMonitorBrightnessMethods" )
			property.WmiSetBrightness( timeout, brightness )	
	}
 	else if ( brightness > 100 )
 	{
 		brightness := 100
 	}
 	else if ( brightness < 0 )
 	{
 		brightness := 0
 	}
}
Dammit! It works. Thanx man

Re: Basic Incremental Laptop Brightness Up/Down

Posted: 05 Mar 2022, 22:36
by mikeyww
Different function. <= is not <.

Re: Basic Incremental Laptop Brightness Up/Down

Posted: 05 Mar 2022, 22:39
by boiler
Good catch. I used the “corrected” version, as you noticed.

Re: Basic Incremental Laptop Brightness Up/Down

Posted: 03 May 2023, 11:32
by GEOVAN
Hello,

Please let me ask regarding the wonderful method described in this post of controlling the screen brightness:
Does this method works and with DESKTOP pc machines that have separate monitor, or it only works with LAPTOPS?

I try it with DESKTOP pc but it was not work, do you have any idea how to do it work with DESKTOP pc, please?

Best Regards

Re: Basic Incremental Laptop Brightness Up/Down

Posted: 03 May 2023, 16:05
by mikeyww
Not sure. Others here may know.

Re: Basic Incremental Laptop Brightness Up/Down

Posted: 20 Oct 2023, 15:20
by supplementfacts
Hi, I've successfully used (a very minor variation of) one of the above scripts for a good long while, but after a recent Windows Update it stopped working:

Code: Select all

; Variables
Increments 		:= 10 ; < lower for a more granular change, higher for larger jump in brightness 
CurrentBrightness 	:= GetCurrentBrightNess()

; Hot Keys
#q::     ChangeBrightness( CurrentBrightness -= Increments ) ; decrease brightness
#w::     ChangeBrightness( CurrentBrightness := 100 ) ; default
#e::     ChangeBrightness( CurrentBrightness += Increments ) ; increase brightness

; Functions
ChangeBrightness( ByRef brightness := 50, timeout = 1 )
{
	if ( brightness >= 0 && brightness <= 100 )
	{
		For property in ComObjGet( "winmgmts:\\.\root\WMI" ).ExecQuery( "SELECT * FROM WmiMonitorBrightnessMethods" )
			property.WmiSetBrightness( timeout, brightness )	
	}
 	else if ( brightness > 100 )
 	{
 		brightness := 100
 	}
 	else if ( brightness < 0 )
 	{
 		brightness := 0
 	}
}

GetCurrentBrightNess()
{
	For property in ComObjGet( "winmgmts:\\.\root\WMI" ).ExecQuery( "SELECT * FROM WmiMonitorBrightness" )
		currentBrightness := property.CurrentBrightness	

	return currentBrightness
}
Now pressing the buttons (Win+Q, Win+W, Win+E) does nothing. I can still control screen brightness using the laptop's built-in method, but I much preferred using these buttons. Does anyone know how to make the script work again?

Re: Basic Incremental Laptop Brightness Up/Down

Posted: 21 Oct 2023, 06:58
by mikeyww
It worked here on Win 10 Pro.