sindresorhus / KeyboardShortcuts

⌨️ Add user-customizable global keyboard shortcuts (hotkeys) to your macOS app in minutes
https://swiftpackageindex.com/sindresorhus/KeyboardShortcuts/documentation/keyboardshortcuts/keyboardshortcuts
MIT License
1.94k stars 181 forks source link

@State is not updated when used in App #156

Closed DorianRudolph closed 9 months ago

DorianRudolph commented 9 months ago

Perhaps I'm using this wrong, but why does the following code not work?

import SwiftUI
import KeyboardShortcuts

@main
struct TestAppApp: App {
    @State var isUnicornMode: Bool = false;
    init() {
        KeyboardShortcuts.onKeyDown(for: .toggleUnicornMode) { [self] in
            isUnicornMode.toggle();
            print("Unicorn \(isUnicornMode)")
        }
    }
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

The print statement is always false. When I use the @MainActor AppState: ObservableObject it works. I'm new to swift programming, but why does the variable in my example not change? Setting = true directly also does not work.

sindresorhus commented 9 months ago

I recommend asking on Stack Overflow. Your question is not directly about this package.