Search code examples
macosswiftplaystation

Using DualShock 4 from OS X


On a whim I bought a Sony DualShock 4 Wireless game controller. Pairing the controller with my mac was easy. The controller works as expected in OpenEmu.

Now I want to use the controller in my own game. In an XCode playground, I tried this:

import Cocoa;
import GameController;

print(GCController.controllers())
print(GCController.controllers().count)

The output is: [] 0

This means that the DualShock controller wasn't registered as a GCController.

Should it?

Is there some kind of initialization that needs to be done before invoking controllers?

An example is Objective-C or Swift would be welcome.


Solution

  • It looks as though DualShock 4 support was added to the Game Controller framework in 2019. This is working for me on macOS 11.1.

    However, it looks as though the controller doesn't show up in the initial call to GCController.controllers(). You need to register for GCControllerDidConnect notifications and then the controller will show up (for both USB and Bluetooth connections).