gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:03:01 25 4
gpt4 key购买 nike

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

最佳答案

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)

关于cocoa - 以编程方式更改 Mac 显示屏亮度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3239749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com