Keyboard listener in SwiftUI: 1-by-1 delivery of keys

Solution 1:

you can use the following to deliver 1 key at a time:

    TextField("key", text: $string)
        .onReceive(string.publisher.last()) { val in
            print("val: \(val)")
        }

EDIT

".... is there any way to register a backspace key press?...", try the following simple extension:

struct ContentView: View {
    
    @State var string = ""
    @State var prevString = ""
    
    var body: some View {
        TextField("key", text: $string).border(.red)
            .onReceive(string.publisher.last()) { val in
                if string.count < prevString.count {
                    print("backspace pressed, do something clever here")
                } else {
                    print("val: \(val)")
                }
                prevString = string
            }
    }
    }