Home > Net >  SwiftUI on macOS, no press indication when using keyboardShortcut
SwiftUI on macOS, no press indication when using keyboardShortcut

Time:06-21

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.

CodePudding user response:

Use capitalised "D", like

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

Tested with Xcode 13.4 / macOS 12.4

  • Related