-
Notifications
You must be signed in to change notification settings - Fork 12
Keyboard brightness API removed by Apple on Macs after 2016 😭 (If you know of any new one, plz help🙏) #1
Comments
Thanks, you're not the only one to report this, but I haven't been able to fix it since it's not reproducible on my 2013 MacBook Pro. |
Ok, well I will see if I can get anywhere with it this weekend. |
Actually I think I fixed it 1d1daf1, pull master and give it a shot when you get a chance. The binaries were just out of date from the c code (where I had removed |
Still having an issue, but got a new error message: |
Hmm the IOKit methods I'm using are all deprecated, it's possible Apple removed support for them when it added upgraded hardware in the 2015+ MBP models. I can try copying over this newer swift code instead for krightness: https://github.com/bhoeting/DiscoKeyboard/blob/b50d1722007aadba1059e5324cc0a572419dfd54/DiscoKeyboard/Backlight.swift |
Same is happening with DiscoKeyboard any leads on how to read KeyboardBrightness ? I have a work around to set it by sending keystrokes but I can't read the value. I'll post it here soon. |
Does |
Yes it does work, ./kbrightness give Edit : Also it would be awesome to be able to set the value as for now the only thing I managed to get working is to send the keystrokes that increase the value by steps. |
Hello! Now if I run blink, it gives me this error: setKeyboardBrightness() error I am on a 2017 MacBook pro nTB |
They changed the driver for the keyboard I'll post here soon how to read the value. But they didn't implemented the set function so we can't set the value without simulating the keyboard keys. |
Got it! Can we somehow implement the "simulating keyboard keys" part? I would love to have the keyboard flash when I get a notification for example :) |
I haven't figured out how to do it by simulating key presses yet, but if you find a way, do post back! This article says there isn't an easy way to do it via AppleScript, and I don't know Swift/ObjC well enough to do it there. fn + F6 on my keyboard is keyboard brightness up, does it vary between macs? |
import IOKit
static func readKeyboardBrigthness() -> Float?
{
let service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleHIDKeyboardEventDriverV2"))
defer
{
IOObjectRelease(service)
}
let ser: CFTypeRef = IORegistryEntryCreateCFProperty(service, "KeyboardBacklightBrightness" as CFString, kCFAllocatorDefault,0).takeUnretainedValue()
if let result = ser as? Float {
return result
}
return nil
} |
// Simulate illumination up
let code = NX_KEYTYPE_ILLUMINATION_UP
let event1 = NSEvent.otherEvent(with: .systemDefined, location: NSPoint.zero, modifierFlags: NSEventModifierFlags(rawValue: 0xa00), timestamp: 0, windowNumber: 0, context: nil, subtype: 8, data1: (Int((code << 16 as Int32) | (0xa << 8 as Int32))), data2: -1)
event1?.cgEvent?.post(tap: .cghidEventTap)
let event2 = NSEvent.otherEvent(with: .systemDefined, location: NSPoint.zero, modifierFlags: NSEventModifierFlags(rawValue: 0xb00), timestamp: 0, windowNumber: 0, context: nil, subtype: 8, data1: (Int((code << 16 as Int32) | (0xb << 8 as Int32))), data2: -1)
event2?.cgEvent?.post(tap: .cghidEventTap)
// Simulate illumination down
let code = NX_KEYTYPE_ILLUMINATION_DOWN
let event1 = NSEvent.otherEvent(with: .systemDefined, location: NSPoint.zero, modifierFlags: NSEventModifierFlags(rawValue: 0xa00), timestamp: 0, windowNumber: 0, context: nil, subtype: 8, data1: (Int((code << 16 as Int32) | (0xa << 8 as Int32))), data2: -1)
event1?.cgEvent?.post(tap: .cghidEventTap)
let event2 = NSEvent.otherEvent(with: .systemDefined, location: NSPoint.zero, modifierFlags: NSEventModifierFlags(rawValue: 0xb00), timestamp: 0, windowNumber: 0, context: nil, subtype: 8, data1: (Int((code << 16 as Int32) | (0xb << 8 as Int32))), data2: -1)
event2?.cgEvent?.post(tap: .cghidEventTap) My previous comment give you the value. Good thing to note is that this is kinda slow. I tried to implement a slider and its too slow for nice results. If you can make something from this please keep me updated. @tymscar here it is ! |
@pirate the is no way to simulate key press for keyboard illumination but you can simulate nsevent sent when those keys are pressed. |
Also now that I'm thinking about this, you may actually be able to listen to quicks change by listening to those events (in my second comment). Like first you read the value with the driver to get the initial value then you only listen to the events. I don't have time to experiment those days this but I will when I can. |
Here is the start of an observer for IOKit. I does not work but I think its not so far : import IOKit
import Foundation
let IOKitListenerNewDataNotificationIdentifier: NSNotification.Name = NSNotification.Name("IOKitListenerNewDataNotification")
let IOKitListenerNewDataNotificationKeyboardBrightnessIdentifier = "IOKitListenerNewDataNotificationKeyboardBrightness"
class IOKitListener: NSObject
{
//var masterPort : mach_port_t?
var isListenerActive : Bool = false
var service : io_registry_entry_t
var notificationPort : IONotificationPortRef?
//var notificationRunLoopSource : CFRunLoopSource?
//var info : Dictionary?
private override init()
{
service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleHIDKeyboardEventDriverV2"))
}
//MARK: Shared Instance
static let shared: IOKitListener = IOKitListener()
func start()
{
guard !isListenerActive else { return }
notificationPort = IONotificationPortCreate(kIOMasterPortDefault)
//let notificationObject : io_object_t
IOServiceAddInterestNotification(notificationPort, service, kIOGeneralInterest, { (_, _, _, _) in IOKitListener.shared.didRefresh() }, nil, nil)
IONotificationPortSetDispatchQueue(notificationPort, DispatchQueue.main)
isListenerActive = true
}
func did (arg1 : UnsafeMutableRawPointer?, arg2 : io_service_t, arg3 : UInt32, arg4 : UnsafeMutableRawPointer?)
{
didRefresh()
}
func stop()
{
guard isListenerActive else { return }
IONotificationPortDestroy(notificationPort)
}
func request()
{
didRefresh()
}
func didRefresh()
{
guard let result = IORegistryEntryCreateCFProperty(service, "KeyboardBacklightBrightness" as CFString, kCFAllocatorDefault,0).takeUnretainedValue() as? Float else { return }
NotificationCenter.default.post(name: IOKitListenerNewDataNotificationIdentifier, object: nil, userInfo: [IOKitListenerNewDataNotificationKeyboardBrightnessIdentifier: result])
}
} |
That looks awesome @Alex293 ! I've however never done macOS dev so I'm not sure how to execute it :( I opened up a new Xcode project and made a cocoa app but it doesn't seem to play well with the code. |
Look for a quick tutorial on the net with just a simple button in a controller then add one of the two part of my second comment. |
Works nicely! :D |
This didn't match my requirements but I'm happy that it still useful to someone ! |
As mentioned in #8 I now have a new mac to test this on, so I can hopefully get this working again sometime in the next few weeks. Post any tips here if you have them. |
Related issue: maxmouchet/LightKit#1 |
As far as I can tell, the keyboard brightness is not present in the IORegistry at all :/ ioreg > before.txt
# changed keyboard brightness via touchbar
ioreg > after.txt
diff before.txt after.txt
# no indication of keyboard-brightness related changes in the difff This means it's either controlled via some other secret API or is not accessible at all. I'll keep investigating, but if I don't find anything we may have to resort to faking the key events as @Alex293 showed above. |
I just reproduced the issue on my Machine:
|
Yeah there's no known fix unfortunately, the registry options that were used to control it in the past are no longer available on new machines. |
Any news on this? I have the same issue. cotfas:mac-keyboard-brightness work$ ./kbrightness |
|
macOS sierra version: 10.12.5 (16F73)
mbp 15-in, 2016
Display controls work fine but keyboard read and write operations fail:
$ ./dbrightness
0.650391
$ ./dbrightness .8
$ ./kbrightness
getLightSensors() error: failed to find ambient light sensor
getKeyboardBrightness() error
0.000000
$ ./kbrightness 1.0
getLightSensors() error: failed to find ambient light sensor
setKeyboardBrightness() error
I will update with more info when I have some time to debug this evening (and will pr if I find the issue).
The text was updated successfully, but these errors were encountered: