调整Mac显示亮度Objective-C?

时间:2012-03-15 14:17:13

标签: objective-c cocoa screen-brightness

有没有办法在Objective-C中调整Mac的显示亮度?我有敏感的眼睛,有时显示器看起来可能更暗。将其与Jeff's blog post结合起来,了解极客和黑暗,它会变得非常烦人。

我认为如果显示器可以设置为数字值,它可以设置为低于常规屏幕亮度。我用Google搜索了所有内容,但所有出现的内容都是关于iPhone的。有没有办法为Mac做到这一点?

1 个答案:

答案 0 :(得分:2)

只是用Google搜索并发现:)

这是如何获得亮度的代码。

    // almost completely from: http://mattdanger.net/2008/12/adjust-mac-os-x-display-brightness-from-the-terminal/
- (float) get_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;
        }
        return brightness;
    }       
    return -1.0;//couldn't get brightness for any display
}

如何设置亮度。

// almost completely from: http://mattdanger.net/2008/12/adjust-mac-os-x-display-brightness-from-the-terminal/
- (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;
        }

        if(brightness > 0.0){
        }else{
        }
    }       

}

我在这里找到了它 - http://www.alecjacobson.com/weblog/?tag=brightness