Better brightness control with on screen display (win 10 tested)

Share your favorite FastKeys commands
Post Reply
noticz
Posts: 5
Joined: Jun 3rd, ’25, 06:06

Post by noticz » Jun 5th, ’25, 11:40

I found that I like the on screen display to appear to tell me where I'm at on my brightness level

Code: Select all

AdjustScreenBrightness(-10)
BrightnessOSD()
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 := 0
    curt := 0 
    for monitor in monitors {
        curt := monitor.CurrentBrightness
        break
    }
    if (curt < minBrightness) {  
        curt := minBrightness
    }
    toSet := curt + step
    if (toSet > 100) {
        return
    }
    if (toSet < minBrightness) {
        toSet := minBrightness
    }
    for method in monMethods {
        method.WmiSetBrightness(1, toSet)
        break
    }
}

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)
}
User avatar
travibe
Posts: 20
Joined: Nov 2nd, ’20, 23:29

Post by travibe » Oct 11th, ’25, 18:24

I used your code and it does in fact show the brightness level gauge thing in the top left of my screen. However, it does not adjust the brightness of my display.
I did get the brightness to adjust using the built in brightness adjustment scripts in Fastkeys library. How can i put these two together so that I can adjust the brightness and see the gauge at the same time?

(Brightness up)
V:=3
#MaxHotkeysPerInterval 10000
VarSetCapacity(GR,1536,0)
DllCall("GetDeviceGammaRamp", UInt,hDC := DllCall("GetDC", UInt,0), UInt,&GR)
Current:=NumGet(GR, 2, "UShort") - 128, DllCall("ReleaseDC", UInt,0, UInt,hDC)
Br:=(Br := Current + V) > 255 ? 255 : Br < 0 ? 0 : Br
Loop, % VarSetCapacity(GR,1536)/6
NumPut((n := (Br+128)*(A_Index-1)) > 65535 ? 65535 : n, GR, 2*(A_Index-1), "UShort")
DllCall("RtlMoveMemory", UInt,&GR+512, UInt,&GR, UInt,512)
DllCall("RtlMoveMemory", UInt,&GR+1024, UInt,&GR, UInt,512)
DllCall("SetDeviceGammaRamp", UInt,hDC := DllCall("GetDC", UInt,0), UInt,&GR)
, DllCall("ReleaseDC", UInt,0, UInt,hDC)


(Brightness down)
V:=-3
#MaxHotkeysPerInterval 10000
VarSetCapacity(GR,1536,0)
DllCall("GetDeviceGammaRamp", UInt,hDC := DllCall("GetDC", UInt,0), UInt,&GR)
Current:=NumGet(GR, 2, "UShort") - 128, DllCall("ReleaseDC", UInt,0, UInt,hDC)
Br:=(Br := Current + V) > 255 ? 255 : Br < 0 ? 0 : Br

Loop, % VarSetCapacity(GR,1536)/6
NumPut((n := (Br+128)*(A_Index-1)) > 65535 ? 65535 : n, GR, 2*(A_Index-1), "UShort")
DllCall("RtlMoveMemory", UInt,&GR+512, UInt,&GR, UInt,512)
DllCall("RtlMoveMemory", UInt,&GR+1024, UInt,&GR, UInt,512)
DllCall("SetDeviceGammaRamp", UInt,hDC := DllCall("GetDC", UInt,0), UInt,&GR)
, DllCall("ReleaseDC", UInt,0, UInt,hDC)
Post Reply