GameControllerKit

0.0.6

GameControllerKit is a Swift package that makes it easy to work with game controllers on iOS, macOS, and tvOS. It provides a simple API to connect to game controllers, read input from them, and control their lights and haptics.
0xWDG/GameControllerKit

What's New

0.0.6

2026-06-14T13:33:02Z

• Add controller mocking and demo application
• Add mock controller state and input APIs
• Add a macOS SwiftUI demo executable
• Improve thumbstick direction detection with a dead zone
• Add tests and update documentation

GameControllerKit

GameControllerKit is a Swift package that makes it easy to work with game controllers on iOS, macOS, and tvOS. It provides a simple API to connect to game controllers, read input from them, and control their lights and haptics.

Swift Package Manager License

Requirements

  • Swift 5.9+ (Xcode 15+)
  • iOS 13+, macOS 10.15+, tvOS 16+

Installation (Pakage.swift)

dependencies: [
    .package(url: "https://github.com/0xWDG/GameControllerKit.git", branch: "main"),
],
targets: [
    .target(name: "MyTarget", dependencies: [
        .product(name: "GameControllerKit", package: "GameControllerKit"),
    ]),
]

Installation (Xcode)

  1. In Xcode, open your project and navigate to FileSwift PackagesAdd Package Dependency...
  2. Paste the repository URL (https://github.com/0xWDG/GameControllerKit) and click Next.
  3. Click Finish.

Usage

import SwiftUI
import GameControllerKit

struct ContentView: View {
    /// The game controller kit
    @State
    var gameController = GameControllerKit()

    /// Log
    @State
    var log: [String] = []

    var body: some View {
        VStack {
            Button {
                gameController.set(color: .GCKRandom)
            } label: {
                Text("Random Color")
            }

            Text("Controller: \(gameController.controller?.productCategory ?? "None"), " +
                 "\((gameController.controllerType ?? .generic).description)")
            Text("Last action:\n\(String(describing: gameController.lastAction)).")

            GCKControllerView(gameController)

            List {
                ForEach(log.reversed(), id: \.self) { text in
                    Text(text)
                }
            }
        }
        .padding()
        .onAppear {
            gameController.set(handler: handler)
            UIApplication.shared.isIdleTimerDisabled = true
        }
    }

    /// Handler
    ///
    /// - Parameters:
    ///   - action: action
    ///   - pressed: is the button pressed?
    ///   - controller: which controller?
    public func handler(
        action: GCKAction,
        pressed: Bool,
        controller: GCKController
    ) {
        log.append(
            "\(String(describing: action))(\(action.position.arrowRepresentation)) \(pressed ? "Pressed" : "Unpressed"), " +
            "Controller #id \(String(describing: controller.playerIndex.rawValue))"
        )

        if action == .buttonA && pressed {
            // set to a random color
            gameController.set(color: .GCKRandom)
        }
    }
}

Demo Application

Run the included macOS SwiftUI demo from the repository:

swift run GameControllerKitDemo

The demo displays connected hardware and can mock controller types and input actions.

Mocking

Mock state is useful for previews, UI development, and tests without controller hardware:

let gameController = GameControllerKit(observesControllers: false)
gameController.mockController(type: .dualSense)
gameController.sendMockAction(.buttonA)

GCKControllerView(gameController)

Image of Usage Demo App

iOS

C65552DF-04CC-493E-AD73-C385A7CEC53C

MacOS

AA801C52-88A1-4326-A5DC-3A04DF491077

tvOS

Screenshot 2024-08-29 at 14 43 51

Contact

🦋 @0xWDG 🐘 mastodon.social/@0xWDG 🐦 @0xWDG 🧵 @0xWDG 🌐 wesleydegroot.nl 🤖 Discord

Interested learning more about Swift? Check out my blog.

Description

  • Swift Tools 5.8.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sun Jun 28 2026 01:46:53 GMT-0900 (Hawaii-Aleutian Daylight Time)