Button.keyboardShortcut(.defaultAction) doesn't properly work on macOS Monterey

In following code example:

struct ContentView: View {
    @State var text: String = "sample text"

    var body: some View {
        VStack {
            TextField("", text: $text)
            Button(action: { print("action performed!") }) {
                Text("Click me")
            }
            .keyboardShortcut(.defaultAction)
        }
        .padding(40)
    }
}

.keyboardShortcut(.defaultAction) that is attached to Button perfectly works on macOs Big Sur(11.6) and I see "action performed!" in Xcode console when tap Enter key on keyboard.

Unfortunately, same example doesn't work on macOs Monterey(12.0.1) and Xcode 13.1. So nothing happens(I don't see any logs) when I tap Enter key on keyboard. Also I noticed that it happens only when focus is set in TextField.

I checked that onSubmit can solve this issue. Unfortunately, I can't use since it's available in macOs 12 or newer but my App has deployment target is macOs 11.

So any workarounds are welcomed.

I have a similar problem. Isn't there a solution?

struct ContentView: View {
   
  @State private var text: String = ""
   
  var body: some View {
    VStack {
      Text(text)
        .padding()
      TextEditor(text: $text)
        .frame(width: 300, height: 200)
        .padding()
       
      Button(action: {
        print(text)
      }, label: {
        Text("Done")
         
      })
        .keyboardShortcut(.defaultAction)
        .padding()
    }
  }
}

I'm having the same problem.

Still an issue.

  • Monterey 12.0.1
  • Xcode 13.1 (13A1030d)

Still happens on Monterey 12.1 and Xcode 13.2

Still happens on Monterey 12.5.1, Xcode 13.4.1, iOS target 15.5 The TextField absorbs first return input and loses its focus state, second return input activates keyboardShortcut modifier. This issue does not occur in iOS target 14.5. 🤷🏻

Button.keyboardShortcut(.defaultAction) doesn't properly work on macOS Monterey
 
 
Q