https://github.com/0xwdg/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.
https://github.com/0xwdg/gamecontrollerkit
0xwdg controller dualsense dualshock game game-development gamedev gamedevelopment games hacktoberfest ios macos playstation spm stadia swift swiftlang switch tvos xbox
Last synced: about 1 year ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/0xwdg/gamecontrollerkit
- Owner: 0xWDG
- License: mit
- Created: 2024-08-19T13:06:40.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-02-20T20:19:32.000Z (over 1 year ago)
- Last Synced: 2025-04-16T02:09:23.017Z (about 1 year ago)
- Topics: 0xwdg, controller, dualsense, dualshock, game, game-development, gamedev, gamedevelopment, games, hacktoberfest, ios, macos, playstation, spm, stadia, swift, swiftlang, switch, tvos, xbox
- Language: Swift
- Homepage: https://0xwdg.github.io/GameControllerKit/
- Size: 920 KB
- Stars: 36
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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.
[](https://swiftpackageindex.com/0xWDG/GameControllerKit)
[](https://swiftpackageindex.com/0xWDG/GameControllerKit)
[](https://swift.org/package-manager)

## Requirements
- Swift 5.9+ (Xcode 15+)
- iOS 13+, macOS 10.15+, tvOS 16+
## Installation (Pakage.swift)
```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 **File** โ **Swift Packages** โ **Add Package Dependency...**
2. Paste the repository URL (`https://github.com/0xWDG/GameControllerKit`) and click **Next**.
3. Click **Finish**.
## Usage
```swift
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()
.environmentObject(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)
}
}
}
```
## Image of Usage Demo App
### iOS

### MacOS

### tvOS

## Contact
๐ฆ [@0xWDG](https://bsky.app/profile/0xWDG.bsky.social)
๐ [mastodon.social/@0xWDG](https://mastodon.social/@0xWDG)
๐ฆ [@0xWDG](https://x.com/0xWDG)
๐งต [@0xWDG](https://www.threads.net/@0xWDG)
๐ [wesleydegroot.nl](https://wesleydegroot.nl)
๐ค [Discord](https://discordapp.com/users/918438083861573692)
Interested learning more about Swift? [Check out my blog](https://wesleydegroot.nl/blog/).