Skip to content

Creating a New Plugin

Emory Dunn edited this page Aug 3, 2021 · 2 revisions

Setting Up Your Project

The recommended way to create a plugin is with SwiftPM. It's up to you whether you'll want to separate your project into a library and executable or not, but for now we'll just be using a single executable.

mkdir counter-plugin
cd counter-plugin
swift package init --type executable

Adding StreamDeck as a Dependency

To use the StreamDeck library in a SwiftPM project, add the following line to the dependencies in your Package.swift file:

.package(name: "StreamDeck", url: "https://github.com/emorydunn/StreamDeckPlugin.git", .branch("main"))

Finally, include "StreamDeck" as a dependency for your executable target:

let package = Package(
    // name, products, etc.
    platforms: [.macOS(.v10_15)],
    dependencies: [
        .package(name: "StreamDeck", url: "https://github.com/emorydunn/StreamDeckPlugin.git", .branch("main")),
        // other dependencies
    ],
    targets: [
        .target(name: "<command-line-tool>", dependencies: [
            "StreamDeck"
        ]),
        // other targets
    ]
)

Creating A Plugin

Subclassing StreamDeckPlugin

Add a new file to your project, we'll call it "Counter.swift", and then add our StreamDeckPlugin subclass:

import Foundation
import StreamDeck

class CounterPlugin: StreamDeckPlugin {

    var counter: Int = 0
    
    override func keyDown(action: String, context: String, device: String, payload: KeyEvent) {
        counter += 1
        setTitle(in: context, to: "\(counter)")
    }
    
}

Running the Plugin

In your main.swift file you'll need to register the plugin's type and call the main function to read the command line arguments from the Steam Deck application. Using the convenience method below lets the PluginManager handle the lifecycle of the plugin and keep the program running.

import Foundation
import StreamDeck

PluginManager.main(plugin: CounterPlugin.self)

Clone this wiki locally