keyboardShortcut modifier is not working with a custom ButtonStyle

Viewed 177

In SwiftUI on macOS you have a modifier to enable shortcuts for a specific control. For example:

struct ContentView: View {
    var body: some View {
        Button("Print message") {
            print("Hello World!")
        }
        .keyboardShortcut("p", modifiers: [.command, .shift])
    }
}

If you run the app and press CMD+SHIFT+p you'll see "Hello world" on your console. Unfortunately as soon as I specify a custom style for the button, the shortcut no longer works:

struct ContentView: View {
    var body: some View {
        Button("Print message") {
            print("Hello World!")
        }
        .buttonStyle(MyButtonStyle())
        .keyboardShortcut("p", modifiers: [.command, .shift])
    }
}

struct MyButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .foregroundColor(.red)
    }
}

Any ideas? Thanks.

0 Answers
Related