import SwiftUI import UIKit /// A hosting controller that answers to the keyboard. /// /// The keys belong here rather than on the controller that owns this one: SwiftUI's views take /// first responder, so the responder chain starts inside this hierarchy, and an override on a /// parent further up was never consulted at all. final class KeyHostingController: UIHostingController { var onKey: ((String) -> Void)? override var canBecomeFirstResponder: Bool { true } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) becomeFirstResponder() } override var keyCommands: [UIKeyCommand]? { Keys.commands(#selector(key(_:))) } @objc private func key(_ command: UIKeyCommand) { guard let input = command.input else { return } onKey?(input) } }