Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjust screen brightness in Mac OS X app

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?

like image 411
ixany Avatar asked Sep 21 '15 09:09

ixany


People also ask

Can you control monitor brightness with software Mac?

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.

What is brightness slider app?

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.

Is there an app to reduce screen brightness?

lumen is among the most outstanding and highly regarded brightness control apps available for Android smartphones.

Why is my Mac not letting me adjust brightness?

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.


1 Answers

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)
like image 159
Eric Aya Avatar answered Sep 29 '22 02:09

Eric Aya