Activating SwiftUI buttons with a custom ButtonStyle using Keyboard Focus on macOS

Hey all,

Currently tested in Xcode 13 / BigSur

It seems the keyboard focus feature of macOS does not work with any Button in SwiftUI that uses a custom ButtonStyle.

You can attach the .focusable() modifier to the Button (or ButtonStyle) to allow Tab / Shift+Tab navigation between buttons, however pressing Space does not execute the button as expected.

Is there something I'm missing? or is this intentionally not supported?

To work around this, all I can think of is using a UIRepresentableView around NSButton, however I'd like to avoid that if possible.

Here is an example View to show the problem: https://gist.github.com/deanPGM/71ddfbe87a47454d220b16809a489f69

Answered by Frameworks Engineer in 679999022

Hey there, you aren't missing anything, but neither is this an intentional change. We are working on getting parity with NSButton as regards keyboard interactions and the like, but unfortunately this is one case that hasn't been handled yet for custom buttons.

Accepted Answer

Hey there, you aren't missing anything, but neither is this an intentional change. We are working on getting parity with NSButton as regards keyboard interactions and the like, but unfortunately this is one case that hasn't been handled yet for custom buttons.

Howdy, Wondering if this was addressed now or if there is a better workaround than wrapping in UIKit elements.

Activating SwiftUI buttons with a custom ButtonStyle using Keyboard Focus on macOS
 
 
Q