Reading display brightness with BrightnessSetter script or anything else that actually works Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Testertime
Posts: 3
Joined: 22 Aug 2017, 16:44

Reading display brightness with BrightnessSetter script or anything else that actually works

22 Aug 2017, 17:06

Hello dear community! I registered here because I had some real trouble with figuring out how to read the display brightness value and hope someone could help me, or at least give me a really good hint.What I have been trying to do is to use this script called BrightnessSetter (https://autohotkey.com/boards/viewtopic ... 1&p=126135 and Github link: https://gist.github.com/qwerty12/4b3f41 ... 5cc15c33c2) in order to simply get the brightness value. But I failed with that, and other alternatives, such as using the Powershell or using this (https://autohotkey.com/boards/viewtopic.php?t=7854) are no alternative, because the first thing would demand higher user rights plus loading the Powershell just to get a simple value would drastically increase the loading time in my script. And the second link with that "Class Monitor" seems more about adjusting color values, and the lack of comments made it look highly confusing and it doesn't seem to read a brightness value anyway (sorry, just my opinion and that's how it looked like for me).

But back to the BrightnessSetter class. There is a part which does likely read the correct brightness value, but my previous attempts at somehow calling it failed:

Code: Select all

_GetCurrentBrightness(schemeGuid, AC, ByRef currBrightness)
	{
		static PowerReadACValueIndex := DllCall("GetProcAddress", "Ptr", BrightnessSetter.hPowrprofMod, "AStr", "PowerReadACValueIndex", "Ptr")
			  ,PowerReadDCValueIndex := DllCall("GetProcAddress", "Ptr", BrightnessSetter.hPowrprofMod, "AStr", "PowerReadDCValueIndex", "Ptr")
		return DllCall(AC ? PowerReadACValueIndex : PowerReadDCValueIndex, "Ptr", 0, "Ptr", schemeGuid, "Ptr", BrightnessSetter._GUID_VIDEO_SUBGROUP(), "Ptr", BrightnessSetter._GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS(), "UInt*", currBrightness, "UInt") == 0
	}
My question: How do I get the brightness value with that in a variable? My own script is basically a GUI with a few sliders, and one of them is for the brightness value. When the script starts, I need to figure out the current brightness value, or else it is ... well, just looking awkward. Setting brightness values isn't a problem, I made use of Nircmd in order to achieve that.

Previously I saw so many other useful posts here and hopefully this topic will be another good one for others who seek to do the same. Thanks in advance for your help! :)
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: Reading display brightness with BrightnessSetter script or anything else that actually works  Topic is solved

22 Aug 2017, 17:50

Hello,

Remember: BrightnessSetter is for laptops only. While some monitors do allow for their brightness to be changed through DDC, to the best of my knowledge that's not integrated into the usual power management subsystem of Windows. Also, remember Windows allows for different brightness values depending on whether the charger is plugged in and what the active power scheme. To try and keep this simple, I'll assume you want the value for the current power scheme and the AC brightness value if the charger is plugged in, and the DC value if not.

Code: Select all

GetCurrentBrightnessLevel()
{
	static GUID_VIDEO_SUBGROUP__, GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__, SystemPowerStatus
	if (!VarSetCapacity(GUID_VIDEO_SUBGROUP__)) {
		VarSetCapacity(SystemPowerStatus, 12)
		,VarSetCapacity(GUID_VIDEO_SUBGROUP__, 16)
		,VarSetCapacity(GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__, 16)
		,NumPut(0x7516B95F, GUID_VIDEO_SUBGROUP__, 0, "UInt"), NumPut(0x4464F776, GUID_VIDEO_SUBGROUP__, 4, "UInt")
		,NumPut(0x1606538C, GUID_VIDEO_SUBGROUP__, 8, "UInt"), NumPut(0x99CC407F, GUID_VIDEO_SUBGROUP__, 12, "UInt")
		,NumPut(0xADED5E82, GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__, 0, "UInt"), NumPut(0x4619B909, GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__, 4, "UInt")
		,NumPut(0xD7F54999, GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__, 8, "UInt"), NumPut(0xCB0BAC1D, GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__, 12, "UInt")
	}


	;get charger status
	if (!DllCall("GetSystemPowerStatus", "Ptr", &SystemPowerStatus))
		return -1

	isOnAc := NumGet(SystemPowerStatus, 0, "UChar") == 1

	; get GUID for active power scheme
	if (DllCall("powrprof\PowerGetActiveScheme", "Ptr", 0, "Ptr*", currSchemeGuid, "UInt"))
		return -1
	
	if (DllCall(isOnAc ? "powrprof\PowerReadACValueIndex" : "powrprof\PowerReadDCValueIndex", "Ptr", 0, "Ptr", currSchemeGuid, "Ptr", &GUID_VIDEO_SUBGROUP__, "Ptr", &GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__, "UInt*", currBrightness, "UInt") == 0, DllCall("LocalFree", "Ptr", currSchemeGuid, "Ptr")) {
		return currBrightness
	}
	
	return -1
}

MsgBox % GetCurrentBrightnessLevel()
Testertime
Posts: 3
Joined: 22 Aug 2017, 16:44

Re: Reading display brightness with BrightnessSetter script or anything else that actually works

25 Aug 2017, 09:59

Thanks a lot, it felt like a honor to get such a helpful response from you about this! ^_^ It works just fine and was what I wanted. Yes, my intention is to get the brightness value of laptops or tablets, not for desktop PCs. If you're curious, my plan was to make a slider in a GUI where you can set the brightness by dragging the slider, and also some number buttons. Without getting the actual current brightness value and moving the slider to its correct position, it looked strange before.
Thank you again!

Return to “Ask For Help”

Who is online

Users browsing this forum: Albireo, Bing [Bot], golfbook, mehdiyat, TAC109 and 175 guests