Basic Incremental Laptop Brightness Up/Down

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
FailSafeNow
Posts: 3
Joined: 17 Nov 2020, 23:54

Basic Incremental Laptop Brightness Up/Down

18 Nov 2020, 00:17

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.
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Basic Incremental Laptop Brightness Up/Down

18 Nov 2020, 06:46

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.
FailSafeNow
Posts: 3
Joined: 17 Nov 2020, 23:54

Re: Basic Incremental Laptop Brightness Up/Down

18 Nov 2020, 12:38

Thank you. I finally managed to get results with the script by querty12. (The other options were no good.)
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Basic Incremental Laptop Brightness Up/Down

18 Nov 2020, 15:22

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
chrismaliszewski
Posts: 1
Joined: 21 Apr 2021, 20:34

Re: Basic Incremental Laptop Brightness Up/Down

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

; 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
}
User avatar
submeg
Posts: 326
Joined: 14 Apr 2017, 20:39
Contact:

Re: Basic Incremental Laptop Brightness Up/Down

04 Aug 2021, 07:42

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!
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
User avatar
LupusWelshie
Posts: 1
Joined: 13 Aug 2021, 19:59

Re: Basic Incremental Laptop Brightness Up/Down

13 Aug 2021, 20:34

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
PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: Basic Incremental Laptop Brightness Up/Down

05 Mar 2022, 22:08

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.
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Basic Incremental Laptop Brightness Up/Down

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)
PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: Basic Incremental Laptop Brightness Up/Down

05 Mar 2022, 22:14

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.
Last edited by PepeLapiu on 05 Mar 2022, 22:23, edited 1 time in total.
PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: Basic Incremental Laptop Brightness Up/Down

05 Mar 2022, 22:21

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
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Basic Incremental Laptop Brightness Up/Down

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
 	}
}
PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: Basic Incremental Laptop Brightness Up/Down

05 Mar 2022, 22:28

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
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Basic Incremental Laptop Brightness Up/Down

05 Mar 2022, 22:36

Different function. <= is not <.
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Basic Incremental Laptop Brightness Up/Down

05 Mar 2022, 22:39

Good catch. I used the “corrected” version, as you noticed.
GEOVAN
Posts: 152
Joined: 03 Mar 2022, 11:12

Re: Basic Incremental Laptop Brightness Up/Down

03 May 2023, 11:32

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
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Basic Incremental Laptop Brightness Up/Down

03 May 2023, 16:05

Not sure. Others here may know.
supplementfacts
Posts: 39
Joined: 15 Jul 2018, 16:29

Re: Basic Incremental Laptop Brightness Up/Down

20 Oct 2023, 15:20

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?
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Basic Incremental Laptop Brightness Up/Down

21 Oct 2023, 06:58

It worked here on Win 10 Pro.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: masheen, mikeyww and 163 guests