In my SwiftUI iOS app, I need to detect which key (and modifier flags – Command, Option, Shift, Control) a user presses, but I don't want to pre-register them using .keyboardShortcut(_:modifiers:).
My use case is that keyboard shortcuts are user-configurable, so I need to capture the actual key + modifier combination dynamically at runtime and perform the appropriate action based on the user’s settings.
Questions:
- What is the recommended way to detect arbitrary key + modifier combinations in SwiftUI on iOS?
- Is there a SwiftUI-native solution for this, or should I rely on
UIPressesEvent and
wrap it withUIViewControllerRepresentable?
- If UIKit bridging is necessary, what is the cleanest pattern for integrating this with SwiftUI views (e.g., Buttons)?
Any official guidance or best practices would be greatly appreciated!