Detecting if Mac has a backlit keyboard

It’s quite easy to detect if Mac has an illuminated keyboard with ioreg at the command line…

ioreg -c IOResources -d 3 | grep '"KeyboardBacklight" =' | sed 's/^.*= //g'

…But how can I programmatically get this IOKit boolean property using the latest Swift? I’m looking for some sample code.

Answered by DTS Engineer in 698269022

Calling I/O Kit from Swift is somewhat challenging. You basically have two strategies here:

  • You can wrap the I/O Kit API in a Swift-friendly wrapper and then use that to accomplish your task.

  • You can just goes straight to your task, resulting in lots of ugly low-level Swift.

I’m a big fan of the first approach. I have my own wrapper but, alas, I’m not able to share that. However, I encourage you to search the ’net to see if someone has created one that you like.

With regards the second approach, the droids you’re looking for are IOServiceMatching, kIOMainPortDefault (or the older kIOMasterPortDefault), IOServiceGetMatchingServices, IOIteratorNext, and IORegistryEntryCreateCFProperty.


Finally, I want to stress that pulling random properties out of the I/O Registry is not the best path to long-term binary compatibility. Our general policy here is that we only support properties that have symbolic constants defined in the headers (most notably IOKitKeys.h, but there are a bunch of others). KeyboardBacklight has no symbolic constant and thus isn’t supported.

Now, I realise that this is not going to stop you )-: However:

  • Please make sure you file an enhancement request for a proper API to get this info, making sure to include a high-level description of your overall goal. Post your bug number here, just for the record.

  • Make sure you code defensively. This property might go away, change meaning, change its type, and so on. Your code must behave reasonable in all such scenarios.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

Calling I/O Kit from Swift is somewhat challenging. You basically have two strategies here:

  • You can wrap the I/O Kit API in a Swift-friendly wrapper and then use that to accomplish your task.

  • You can just goes straight to your task, resulting in lots of ugly low-level Swift.

I’m a big fan of the first approach. I have my own wrapper but, alas, I’m not able to share that. However, I encourage you to search the ’net to see if someone has created one that you like.

With regards the second approach, the droids you’re looking for are IOServiceMatching, kIOMainPortDefault (or the older kIOMasterPortDefault), IOServiceGetMatchingServices, IOIteratorNext, and IORegistryEntryCreateCFProperty.


Finally, I want to stress that pulling random properties out of the I/O Registry is not the best path to long-term binary compatibility. Our general policy here is that we only support properties that have symbolic constants defined in the headers (most notably IOKitKeys.h, but there are a bunch of others). KeyboardBacklight has no symbolic constant and thus isn’t supported.

Now, I realise that this is not going to stop you )-: However:

  • Please make sure you file an enhancement request for a proper API to get this info, making sure to include a high-level description of your overall goal. Post your bug number here, just for the record.

  • Make sure you code defensively. This property might go away, change meaning, change its type, and so on. Your code must behave reasonable in all such scenarios.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Detecting if Mac has a backlit keyboard
 
 
Q