'Bluetooth keyboard input in Swift 5 SwiftUI app (versus UIKit)

I'm porting an iOS app from UIKit to SwiftUI. In this app, I need to capture and respond to keyboard input from a Bluetooth keyboard.

In the UIKit app, I was doing this within my ViewController:

class ViewController: UIViewController {

...

      override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
        guard let key = presses.first?.key else { return }
        let thisKey = key.keyCode.rawValue
        //Do something with `thisKey`
    }

}
...

I'm not sure how to do this in SwiftUI. I see that there is a .command modifier which can accept specific keys, but what I need is the keyCode of the key that was pressed.

Any ideas?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source