Skip to content

Instantly share code, notes, and snippets.

@krrr
Last active March 23, 2024 18:09
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krrr/3c3f1747480189dbb71f to your computer and use it in GitHub Desktop.
Save krrr/3c3f1747480189dbb71f to your computer and use it in GitHub Desktop.
Windows screen brightness fine tune (autohotkey)
#,::
AdjustScreenBrightness(-3)
Return
#.::
AdjustScreenBrightness(3)
Return
AdjustScreenBrightness(step) {
service := "winmgmts:{impersonationLevel=impersonate}!\\.\root\WMI"
monitors := ComObjGet(service).ExecQuery("SELECT * FROM WmiMonitorBrightness WHERE Active=TRUE")
monMethods := ComObjGet(service).ExecQuery("SELECT * FROM wmiMonitorBrightNessMethods WHERE Active=TRUE")
minBrightness := 5 ; level below this is identical to this
for i in monitors {
curt := i.CurrentBrightness
break
}
if (curt < minBrightness) ; parenthesis is necessary here
curt := minBrightness
toSet := curt + step
if (toSet > 100)
return
if (toSet < minBrightness)
toSet := minBrightness
for i in monMethods {
i.WmiSetBrightness(1, toSet)
break
}
}
@TPosition
Copy link

Thanks for your code, could you make it display the built-in light indicator like image below?
image

@Ahmad-f79
Copy link

Ahmad-f79 commented Mar 27, 2021

@TPosition

AdjustScreenBrightness(step) {
	static service := "winmgmts:{impersonationLevel=impersonate}!\\.\root\WMI"
	monitors := ComObjGet(service).ExecQuery("SELECT * FROM WmiMonitorBrightness WHERE Active=TRUE")
	monMethods := ComObjGet(service).ExecQuery("SELECT * FROM wmiMonitorBrightNessMethods WHERE Active=TRUE")
	for i in monitors {
		curr := i.CurrentBrightness
		break
	}
	toSet := curr + step
	if (toSet < 10)
		toSet := 10
	if (toSet > 100)
		toSet := 100
	for i in monMethods {
		i.WmiSetBrightness(1, toSet)
		break
	}
	BrightnessOSD()
}
BrightnessOSD() {
	static PostMessagePtr := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandle", "Str", "user32.dll", "Ptr"), "AStr", A_IsUnicode ? "PostMessageW" : "PostMessageA", "Ptr")
	 ,WM_SHELLHOOK := DllCall("RegisterWindowMessage", "Str", "SHELLHOOK", "UInt")
	static FindWindow := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandle", "Str", "user32.dll", "Ptr"), "AStr", A_IsUnicode ? "FindWindowW" : "FindWindowA", "Ptr")
	HWND := DllCall(FindWindow, "Str", "NativeHWNDHost", "Str", "", "Ptr")
	IF !(HWND) {
		try IF ((shellProvider := ComObjCreate("{C2F03A33-21F5-47FA-B4BB-156362A2F239}", "{00000000-0000-0000-C000-000000000046}"))) {
			try IF ((flyoutDisp := ComObjQuery(shellProvider, "{41f9d2fb-7834-4ab6-8b1b-73e74064b465}", "{41f9d2fb-7834-4ab6-8b1b-73e74064b465}"))) {
				DllCall(NumGet(NumGet(flyoutDisp+0)+3*A_PtrSize), "Ptr", flyoutDisp, "Int", 0, "UInt", 0)
				 ,ObjRelease(flyoutDisp)
			}
			ObjRelease(shellProvider)
		}
		HWND := DllCall(FindWindow, "Str", "NativeHWNDHost", "Str", "", "Ptr")
	}
	DllCall(PostMessagePtr, "Ptr", HWND, "UInt", WM_SHELLHOOK, "Ptr", 0x37, "Ptr", 0)
}

BrightnessOSD() credits: YashMaster and qwerty12

@samuelcaldas
Copy link

it was all i was looking for

@erbanku
Copy link

erbanku commented Oct 12, 2021

Modified a little bit, so that we can change brightness via Shift + Mouse Wheel (Scroll up & down)

+WheelDown::
  AdjustScreenBrightness(-3)
  Return
  
+WheelUp::
  AdjustScreenBrightness(3)
  Return
  
AdjustScreenBrightness(step) {
    service := "winmgmts:{impersonationLevel=impersonate}!\\.\root\WMI"
    monitors := ComObjGet(service).ExecQuery("SELECT * FROM WmiMonitorBrightness WHERE Active=TRUE")
    monMethods := ComObjGet(service).ExecQuery("SELECT * FROM wmiMonitorBrightNessMethods WHERE Active=TRUE")
    minBrightness := 5  ; level below this is identical to this

    for i in monitors {
        curt := i.CurrentBrightness
        break
    }
    if (curt < minBrightness)  ; parenthesis is necessary here
        curt := minBrightness
    toSet := curt + step
    if (toSet > 100)
        return
    if (toSet < minBrightness)
        toSet := minBrightness
        
    

    for i in monMethods {
        i.WmiSetBrightness(1, toSet)
        break
    }
}

@erbanku
Copy link

erbanku commented Oct 12, 2021

BTW share an AHK code for changing volume via Windows Key + Mouse Wheel (Scroll up & down) , enjoy.

#WheelUp::Volume_Up
#WheelDown::Volume_Down

@dpw1
Copy link

dpw1 commented Oct 19, 2021

BTW share an AHK code for changing volume via Windows Key + Mouse Wheel (Scroll up & down) , enjoy.

#WheelUp::Volume_Up
#WheelDown::Volume_Down

That is very helpful, @erbanku , thank you!

@Write
Copy link

Write commented Oct 20, 2021

Is there a way to support "Regular" monitor ? I know that I can do it with this soft : https://github.com/blackholeearth/Win10_BrightnessSlider
I would love it to for this script to support ddc/ci monitors idk if that easy ¯\(ツ)

As long as we are in useful scripts, here's my script to change volume on task bar hover + scroll

I also use Ctrl + Scroll + Hover Task bar to change Brightness with flux and Color With Alt+ Scroll + Hover Task Bar
(I currently use f.lux to set the brightness but AFAIK it only fake a lower brightness, it doesn't actually reduce the backlighting)

;-----------------------------------------------------------------------------
; Change volume using scroll wheel over taskbar / sound
;-----------------------------------------------------------------------------
#If MouseIsOver("ahk_class Shell_TrayWnd") OR MouseIsOver("ahk_class Shell_SecondaryTrayWnd")

    ; Flux Brightness
    ; DOWN
    ^WheelDown::
        SendInput, {alt down}{PgDn down}{PgDn up}{alt up}
    Return

    ; UP
    ^WheelUp::
        SendInput, {alt down}{PgUp down}{PgUp up}{alt up}
    Return
    ; End Brightness

    ; Flux colors
    ; DOWN
    !WheelDown::
        SendInput, {Alt down}{Shift Down}{PgDn down}{PgDn up}{Shift Up}{alt up}
    Return

    ; UP
    !WheelUp::
        SendInput, {Alt down}{Shift Down}{PgUp down}{PgUp up}{Shift Up}{alt up}
    Return
    ; End colors


    WheelUp::
    Send {Volume_Up}
    Send {Volume_Up}
    return

    WheelDown::
    Send {Volume_Down}
    Send {Volume_Down}
    Return

    MButton::
    Send {Volume_Mute}
    Return

    MouseIsOver(WinTitle) {
        MouseGetPos,,, Win
        return WinExist(WinTitle . " ahk_id " . Win)
    }
#If
Return

@krrr
Copy link
Author

krrr commented Oct 30, 2021

I'm not using this script anymore, and recommend this opensource uwp app: https://github.com/xanderfrangos/twinkle-tray

@GameEntity903
Copy link

Hey, I saw this script and I was wondering how to get the current brightness of the screen outside the function.

@Rabelaiss
Copy link

@Ahmad-f79 Does it work on Windows 11 too? I'm trying it but it shows the volume indicator instead
image

@erbanku
Copy link

erbanku commented Dec 20, 2022

@Ahmad-f79 Does it work on Windows 11 too? I'm trying it but it shows the volume indicator instead image

The below code works fine in Windows 11. You can adjust the brightness using Shift+Mouse Scroller Up/Down.

;Shift + Mouse Scroller

+WheelDown::
    AdjustScreenBrightness(-5)
Return

+WheelUp::
    AdjustScreenBrightness(5)
Return

AdjustScreenBrightness(step) {
    service := "winmgmts:{impersonationLevel=impersonate}!\\.\root\WMI"
    monitors := ComObjGet(service).ExecQuery("SELECT * FROM WmiMonitorBrightness WHERE Active=TRUE")
    monMethods := ComObjGet(service).ExecQuery("SELECT * FROM wmiMonitorBrightNessMethods WHERE Active=TRUE")
    minBrightness := 5 ; level below this is identical to this

    for i in monitors {
        curt := i.CurrentBrightness
        break
    }
    if (curt < minBrightness) ; parenthesis is necessary here
        curt := minBrightness
    toSet := curt + step
    if (toSet > 100)
        return
    if (toSet < minBrightness)
        toSet := minBrightness

    for i in monMethods {
        i.WmiSetBrightness(1, toSet)
        break
    }
}

@Rabelaiss
Copy link

@erbanku Thank you, yes I'm using that piece of code, but it does not let the brightness bar popup appear
image

I tried using the code posted by @Ahmad-f79 but it doesn't work (I'm on Windows 11)

@venturqx
Copy link

@Rabelaiss Did you find a solution to display the Brightness OSD on W11 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment