You should use controller profiles to map physical controls to game inputs.
Controllers are automatically discovered, a physical controller is represented by a GCController object, which ‘profiles’ the controllers controls such as GCGamepad, extendedGamepad etc. You should check which controls are registered to each controller. From the documentation on Discovering And Connecting Controllers:
“After your app has finished launching, the operating system
automatically creates a list of connected controllers. Call
the controllers class method to retrieve an array
of GCController objects for all connected controllers.”
In apples sample code they register for .GCControllerDidConnect Notifications and cast the notification object as a GCController instance to a function that set’s up the controls if they exist, parse the controller and assign the corresponding handler method:
NotificationCenter.default.addObserver(self, selector: #selector(GameViewController.handleControllerDidConnectNotification(_:)), name: .GCControllerDidConnect, object: nil)
@objc func handleControllerDidConnectNotification(_ notification: NSNotification) {
let gameController = notification.object as! GCController
registerCharacterMovementEvents(gameController)
}
private func registerCharacterMovementEvents(_ gameController: GCController) {
//…
// Gamepad D-pad
if let gamepad = gameController.gamepad {
gamepad.dpad.valueChangedHandler = movementHandler
}
// Extended gamepad left thumbstick
if let extendedGamepad = gameController.extendedGamepad {
extendedGamepad.leftThumbstick.valueChangedHandler = movementHandler
}
//…
}