bauerMusic
bauerMusic

Reputation: 6156

SwiftUI on macOS, no press indication when using keyboardShortcut

In AppKit, when using keyboard shortcut, the button would visually press when using the shortcut key. In SwiftUI, using keyboardShortcut(_:modifiers:) works, but there's no indication on the button.
Also, I seems to remember reading somewhere that using keyboardShortcut(_:modifiers:) adds the modifier to the button ("Press (⌘ D)"). Is that only for menus?

struct ContentView: View {
    var body: some View {
        Button("Press") {
            print("pressed")
        }
        .keyboardShortcut("d", modifiers: .command)
            .padding()
    }
}

Again, this works, but there's no press indication when using the shortcut.

Upvotes: 0

Views: 99

Answers (1)

Asperi
Asperi

Reputation: 257533

Use capitalised "D", like

    Button("Press") {
        print("pressed")
    }
    .keyboardShortcut("D")    // << here !!

Tested with Xcode 13.4 / macOS 12.4

Upvotes: 1

Related Questions