swift – Responding to key presses in SwiftUI on iOS

0
56


SwiftUI iOS – how to capture hardware key events – this is what I have tried. But unfortunately for my scenario I am unable to modify the solution to fit my needs.

I have a content view which is not the entry point of the app. Inside the content view I have other views. The overall structure looks something like this.

struct Content: View {
    @State keyPressed: Bool = false 
    var body: some View {
        VStack {
            GeometryReader { proxy in 
                innerContent
                    . <-- // Here I tried to apply the solution from the given link.
            }.frame(width: 150, height: 300)
            Spacer()
        }
    }
}

When I tried to apply the solution that I linked, the layout of the app was a mess. Because I essentially replaced the whole view by UIHostingController with the same view inside. Any way to overcome this, or any other way how to respond to key presses on iPadOS specifically when the view we need to respond on is not the entry view ?