soffes / HotKey

Simple global shortcuts in macOS
MIT License
921 stars 82 forks source link

Support for SwiftUI #32

Open michael-eder opened 3 years ago

michael-eder commented 3 years ago

Do you plan to support SwiftUI (or even extend the documentation, how to integrate into SwiftUI)?

glouel commented 3 years ago

Not author, but you can use the code in an init() of a view (or app) directly, just make sure, as was pointed in another issue, to make the hotkey reference a class variable.

Looks like this in the App :

struct MyApp: App {
    let hotKey: HotKey

    init() {
        // Setup hot key for ⌥⌘R
        hotKey = HotKey(key: .r, modifiers: [.command, .option])

        hotKey.keyDownHandler = {
            print("Pressed at \(Date())")
        }
    }
    ...
}
Aayush9029 commented 3 years ago

I've used .onAppear and it seems to work well