Detect if the active keyboard has a physical `Fn` key in Swift

201 Views Asked by At

I want to use the Fn modifier key if available, and if not, offer the user a different modifier. But only offer it to users without a Fn key. Some external keyboard do not have a Fn key.

Is there any way to detect whether the active keyboard has a physical Fn key?

(I am not asking how to detect a Fn key press)

I'm looking for a solution in Swift that works within a sandboxed app.

1

There are 1 best solutions below

0
On

To answer the question I'll explain the strategy but even if there is a way, it's probably not worth going for it.

Some cases to consider:

  1. It might be a MacBook
  2. It might be a MacBook with more than one keyboard attached
  3. We might be dealing with an A1048 which doesn't have an Fn key
  4. There might be a headless Mac Mini with an on-screen keyboard which does have an Fn key, but doesn't fire detectable events when pressed
  5. There might be a 3rd party bluetooth keyboard attached (not Apple) which has an Fn key, but the strokes applied with that modifier will be emulated, so it will not be detectable

Across the board we don't have any APIs to check for physical button presence sadly, but we do have:

Obviously considering that these APIs are available in a sandboxed environment, we can make an exhaustive database. We can also limit the list to popular models to make it smaller.


From a UX perspective though, here is an example. On Windows keyboards in the past we used to have this key:

windows menu key

But not all keyboards had it, so to compensate, Shift+F10 does the same. Still does in Windows 11.

Or if you're writing a notification for the user, it's very common to show the alternatives:

chrome devtools zooming shortcuts

I'd recommend going with a similar approach instead.