PyObjC GameController Framework
PyObjC is a bridge between Python and Apple's Objective-C frameworks on macOS. The `pyobjc-framework-gamecontroller` package provides Python wrappers for the GameController framework, enabling Python applications to interact with game controllers. The library is actively maintained, with releases frequently aligning with new macOS SDKs and Python versions. Current version is 12.1.
Warnings
- breaking PyObjC 12.0 dropped support for Python 3.9. PyObjC 11.0 dropped support for Python 3.8. Ensure your Python version meets the `requires_python` specification.
- breaking PyObjC 11.1 changed how initializer methods (e.g., `init` family) handle references to align with Automatic Reference Counting (ARC) behavior. These methods now correctly model stealing a reference to `self` and returning a new one. Code relying on the previous, less accurate reference counting behavior for initializers might experience issues.
- gotcha PyObjC and its framework wrappers are built exclusively for macOS. This library will not function on Linux, Windows, or other operating systems.
- gotcha PyObjC 10.3 temporarily removed support for calling `__init__` when a class or its superclasses provided a custom `__new__` method. While partially reverted in 10.3.1, users with custom `__new__` implementations should test carefully and adapt `__init__` logic if needed.
- gotcha PyObjC 11.0 introduced experimental support for Python 3.13's free-threading (PEP 703). While this is a significant enhancement, it is noted as experimental and may have limitations or require careful usage in multithreaded scenarios.
Install
-
pip install pyobjc-framework-gamecontroller
Imports
- GCController
from GameController import GCController
Quickstart
import objc
from GameController import GCController
def list_game_controllers():
controllers = GCController.controllers()
if controllers:
print(f"Found {len(controllers)} game controllers:")
for controller in controllers:
vendor_name = controller.vendorName() or 'Unknown Vendor'
print(f" - {vendor_name} {controller.productCategory()} (Type: {controller.controllerType()})")
if controller.extendedGamepad():
print(" Extended Gamepad available.")
elif controller.microGamepad():
print(" Micro Gamepad available.")
else:
print("No game controllers found.")
if __name__ == "__main__":
list_game_controllers()
# In a persistent application, you would typically register for
# GCControllerDidConnectNotification and GCControllerDidDisconnectNotification
# to dynamically track controller connections/disconnections.