以编程方式更改Mac显示屏亮度

时间:2010-07-13 17:30:56

标签: cocoa macos brightness

如何从cocoa应用程序更改Mac显示亮度?

4 个答案:

答案 0 :(得分:13)

CGDisplayIOServicePortdeprecated in OS 10.9 - 因此您必须使用IOServiceGetMatchingServices获取IODisplaySetFloatParameter的服务参数。这是一个基本功能,可以查找名为“display”的服务并更改其亮度。

- (void) setBrightnessTo: (float) level
{

    io_iterator_t iterator;
    kern_return_t result = IOServiceGetMatchingServices(kIOMasterPortDefault,
                                                        IOServiceMatching("IODisplayConnect"),
                                                        &iterator);

    // If we were successful
    if (result == kIOReturnSuccess)
    {
        io_object_t service;
        while ((service = IOIteratorNext(iterator))) {
            IODisplaySetFloatParameter(service, kNilOptions, CFSTR(kIODisplayBrightnessKey), level);

            // Let the object go
            IOObjectRelease(service);

            return;
        }
    }
}

在Swift中(通过@Dov):

private func setBrightnessLevel(level: Float) {
    var iterator: io_iterator_t = 0

    let result = IOServiceGetMatchingServices(kIOMasterPortDefault,
                                              IOServiceMatching("IODisplayConnect").takeUnretainedValue(),
                                              &iterator)

    if result == kIOReturnSuccess {
        var service: io_object_t = 1

        for ;; {
            service = IOIteratorNext(iterator)

            if service == 0 {
                break
            }

            IODisplaySetFloatParameter(service, 0, kIODisplayBrightnessKey, level)
            IOObjectRelease(service)
        }
    }
}

(代码为open source of course

答案 1 :(得分:3)

来自Alec Jacobson的Brightness Menu源代码:

- (void) set_brightness:(float) new_brightness {
CGDirectDisplayID display[kMaxDisplays];
CGDisplayCount numDisplays;
CGDisplayErr err;
err = CGGetActiveDisplayList(kMaxDisplays, display, &numDisplays);

if (err != CGDisplayNoErr)
    printf("cannot get list of displays (error %d)\n",err);
for (CGDisplayCount i = 0; i < numDisplays; ++i) {
    CGDirectDisplayID dspy = display[i];
    CFDictionaryRef originalMode = CGDisplayCurrentMode(dspy);
    if (originalMode == NULL)
        continue;
            io_service_t service = CGDisplayIOServicePort(dspy);

    float brightness;
    err= IODisplayGetFloatParameter(service, kNilOptions, kDisplayBrightness,
                                    &brightness);
    if (err != kIOReturnSuccess) {
        fprintf(stderr,
                "failed to get brightness of display 0x%x (error %d)",
                (unsigned int)dspy, err);
        continue;
    }

    err = IODisplaySetFloatParameter(service, kNilOptions, kDisplayBrightness,
                                     new_brightness);
    if (err != kIOReturnSuccess) {
        fprintf(stderr,
                "Failed to set brightness of display 0x%x (error %d)",
                 (unsigned int)dspy, err);
        continue;
    }
}       

}

答案 2 :(得分:2)

扩展Alex的答案:

在带有Swift3的Xcode8 beta3中,代码更加简化了。

 private func setBrightness(level: Float) {
    let service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IODisplayConnect"))

    IODisplaySetFloatParameter(service, 0, kIODisplayBrightnessKey, level)
    IOObjectRelease(service)
}

答案 3 :(得分:1)

Xcode 8 beta 6中的

无法编译:

IODisplaySetFloatParameter(service, 0, kIODisplayBrightnessKey, level)

无法转换类型&#39;字符串&#39;的值预期参数类型&#39; CFString!&#39;

所以让&#39;施展它:

IODisplaySetFloatParameter(service, 0, kIODisplayBrightnessKey as CFString!, level)