Now with iOS 17.0+, MacOS 14.0+, there's an onKeyPress()
modifier to do that, while allowing us to stick to a TextEditor
https://developer.apple.com/documentation/swiftui/view/onkeypress(_:action:)
// Handle keyboard events for Enter and Shift+Enter
.onKeyPress(keys: [.return]) { event in
if event.modifiers == .shift {
// let the dispatch continue, so TextEditor handles ENTER -> adds a new line
return .ignored
}
// otherwise, handle the key press -> submit
handleSubmit()
return .handled
}