How to submit TextField when hitting return/enter key?

We are trying to submit a login form on enter by using onCommit. It works but it seems that it is not a good fit because the onCommit action triggers whenever the field loses focus, so simply clicking between the email/password fields will cause the form to be submitted.

Here is a playground showing the issue:

Code Block
import SwiftUI
import PlaygroundSupport
struct LoginView: View {
@State public var email: String
@State public var password: String
var body: some View {
TextField("Email", text: $email)
SecureField("Password", text: $password, onCommit: onLoginAction)
Button("Login", action: onLoginAction)
}
}
func onLoginAction() {
print("submitting login")
}
PlaygroundPage.current.setLiveView(LoginView(email: "a@b.cc", password: "secret"))

Answered by robnotyou in 667518022
We now have the "keyboardShortcut" modifier.
Try this:

Code Block
Button("Login", action: onLoginAction)
    .keyboardShortcut(.defaultAction)

you could just try this:

SecureField("Password", text: $password)
Accepted Answer
We now have the "keyboardShortcut" modifier.
Try this:

Code Block
Button("Login", action: onLoginAction)
    .keyboardShortcut(.defaultAction)

Did that work for you, @sparta?
How to submit TextField when hitting return/enter key?
 
 
Q