Ambient Light Sensor, Display, and Keyboard brightness control in Swift.
Access the ambient light sensors, and control MacBook’s display & keyboard brightness in Swift.
Warning: While it should be safe to play with the backlight, I am not responsible for any damages made to a computer using this code.
# Podfile
pod "LightKit"
# Cartfile
github "maxmouchet/LightKit"
import LightKit
let lk = LightKit()! // May be nil if it failed to open I/Os.
Note : Most methods and properties are returning optional as I/Os requests may fail if sensors and controllers could not be found.
let lightSensorsReadings = lk.lightSensors
print("Left sensor: \(lightSensorsReadings?.left).")
print("Right sensor: \(lightSensorsReadings?.right).")
print("Display brightness is \(lk.displayBrightness)")
lk.setDisplayBrightness(0.8) // A value between 0 and 1.
lk.setDisplayPower(false) // Put display to sleep
lk.setDisplayPower(true) // Wake up display
print("Keyboard brightness is \(lk.keyboardBrightness)")
lk.setKeyboardBrightness(0.8) // A value between 0 and 1.