79510205

Date: 2025-03-14 20:55:51
Score: 1
Natty:
Report link

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
}
Reasons:
  • Probably link only (1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: blackhogz