osx - Adjust screen brightness in Mac OS X app -


i want control brightness of main-screen within mac os x app (like f1/f2 buttons). in ios, there's this:

uiscreen.mainscreen().brightness = cgfloat(0.5) 

in osx have nsscreen, nice find out main-screen is, misses .brightness method.

so how can adjust monitor brightness using swift on osx?

there's no such nice api doing on os x.

we have use ioservicegetmatchingservices find "iodisplayconnect" (the display device) use kiodisplaybrightnesskey set 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) 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -