I want to control the brightness of the main-screen within my Mac OS X app (like the F1/F2 buttons). In iOS, there's something like this:
UIScreen.mainScreen().brightness = CGFloat(0.5)
In OSX we have NSScreen, which is nice to find out what the main-screen is, but it misses the .brightness
method.
So how can I adjust the monitor brightness using Swift on OSX?
Yes, Apple Silicon (M1, M1 Pro, M1 Max, M1 Ultra & M2) Macs are supported, and can control external display brightness, constrast and volume using DDC. You can also use DisplayBuddy to adjust the iMac screen brightness or the Apple Studio Display / Pro Display XDR Brightness.
Brightness Slider gives you total. control over your screen's brightness settings, allowing in. particular for a really smooth transition between low light and total. darkness.
lumen is among the most outstanding and highly regarded brightness control apps available for Android smartphones.
Check your keyboard settings: On your Mac, go to System Preferences > Keyboard > Keyboard tab. Check to see if the “use F1, F2, etc. keys standard function keys” box is checked. If it is, uncheck it, and then try to adjust your brightness again.
There's no such nice API for doing this on OS X.
We have to use IOServiceGetMatchingServices
to find "IODisplayConnect"
(the display device) then use the kIODisplayBrightnessKey
to set the brightness:
func setBrightnessLevel(level: Float) {
var iterator: io_iterator_t = 0
if IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching("IODisplayConnect"), &iterator) == kIOReturnSuccess {
var service: io_object_t = 1
while service != 0 {
service = IOIteratorNext(iterator)
IODisplaySetFloatParameter(service, 0, kIODisplayBrightnessKey, level)
IOObjectRelease(service)
}
}
}
setBrightnessLevel(0.5)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With