Skip to content

Instantly share code, notes, and snippets.

@krrr
Last active November 12, 2024 21:47
Show Gist options
  • 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
}
}
@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 ?

@sameert89
Copy link

sameert89 commented Apr 26, 2024

Updated Script for AHK v2. I could not get the OS brightness popup to work, So I implemented my own tooltip.

; Brightness Control ;

+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 := 0

    curt := 0 
    for monitor in monitors {
        curt := monitor.CurrentBrightness
        break
    }
    if (curt < minBrightness) {  
        curt := minBrightness
    }
    toSet := curt + step
    if (toSet > 100) {
        ToolTip "Brightness: 100%"
        return
    }
    if (toSet < minBrightness) {
        toSet := minBrightness
    }

    for method in monMethods {
        method.WmiSetBrightness(1, toSet)
        break
    }
    ToolTip "Brightness: " toSet "%"
    SetTimer RemoveToolTip, -1000  ; Remove tooltip after 1 second
}

RemoveToolTip(){
	ToolTip  ; Clears the tooltip
	return
}

@noticz
Copy link

noticz commented Nov 10, 2024

@venturqx, @Rabelaiss
Here is my updated code with windows GUI brightness overlay... working on my Lenovo laptop with Win 10.

#SingleInstance, Force

; Mouse scroll up and down holding shift
+WheelDown::
{
	AdjustScreenBrightness(-10)
	BrightnessOSD()
	return
}

+WheelUp::
{
	AdjustScreenBrightness(10)
	BrightnessOSD()
	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 := 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)
}

@uvvvvs
Copy link

uvvvvs commented Nov 12, 2024

If there was a way to adjust the brightness through "hovering the mouse on the top part of screen and then using the mouse wheel", instead of pressing the Shift key on the keyboard while using the mouse wheel would be a great, speedy action and yet more practical to my mind.
Could the current code be written this way? I did not have the knowledge to write it myself :(

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