Adjust screen brightness in Mac OS X app

爱⌒轻易说出口 提交于 2019-11-30 22:09:47

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!