SwiftUI如何在keyUp macOS事件上更改状态?

时间:2020-04-16 21:32:27

标签: ios swift macos swiftui

我正在尝试从窗口检测到keyUp时更改状态值。但是,它似乎对我不起作用。这是我的代码

/// Window
class Window: NSWindow {
  override func keyUp(with event: NSEvent) {
    (contentView as! NSHostingView<ContentView>).rootView.keyPressed(with: event)
  }
}

/// ContentView
struct ContentView {
  @State var index: Int = 0
  var body: some View { ... }

  func keyPressed(with event: NSEvent) {
    self.index = self.index + 1
  }
}

我使用调试器对其进行了测试,显然,成功调用了keyPressed,但是索引设置不正确。有人知道为什么吗?或在macOS的SwiftUI中听键盘的正确策略是什么?

1 个答案:

答案 0 :(得分:6)

这里是可能方法的演示。经过Xcode 11.4 / macOS 10.15.4的测试

这个想法是通过SwiftUI View通过注入的发布者将定制的NSWindow加入到环境值中,从而生成自定义NSWindow。这样就可以在任何级别的视图层次上侦听/处理事件。

下面是完整的模块(AppDelegate.swift)代码。另请参见代码中的有用注释。

import Cocoa
import SwiftUI
import Combine

// Environment key to hold even publisher
struct WindowEventPublisherKey: EnvironmentKey {
    static let defaultValue: AnyPublisher<NSEvent, Never> = 
        Just(NSEvent()).eraseToAnyPublisher() // just default stub
}


// Environment value for keyPublisher access
extension EnvironmentValues {
    var keyPublisher: AnyPublisher<NSEvent, Never> {
        get { self[WindowEventPublisherKey.self] }
        set { self[WindowEventPublisherKey.self] = newValue }
    }
}

// Custom window holding publisher and sending events to it. In general 
// it can be any event, but for originated question we limit to keyUp events
class Window: NSWindow {
    private let publisher = PassthroughSubject<NSEvent, Never>() // private

    var keyEventPublisher: AnyPublisher<NSEvent, Never> { // public
        publisher.eraseToAnyPublisher()
    }

    override func keyUp(with event: NSEvent) {
        publisher.send(event)
    }
}

// Root demo view
struct DemoKeyPressedView: View {
    @Environment(\.keyPublisher) var keyPublisher // << access to publisher

    @State private var index: Int = 0
    var body: some View {
        Text("Demo \(index)")
            .onReceive(keyPublisher) { event in // << listen to events
                self.keyPressed(with: event)
            }
    }

    func keyPressed(with event: NSEvent) {
        self.index += 1
    }
}

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    var window: Window!


    func applicationDidFinishLaunching(_ aNotification: Notification) {

        // Create the custom window
        window = Window(
            contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
            styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
            backing: .buffered, defer: false)
        window.center()
        window.setFrameAutosaveName("Main Window")

        // Create the SwiftUI view that provides the window contents.
        let contentView = DemoKeyPressedView()
            .frame(minWidth: 400, maxWidth: .infinity, maxHeight: .infinity)
            .environment(\.keyPublisher, window.keyEventPublisher) // inject publisher

        window.contentView = NSHostingView(rootView: contentView)
        window.makeKeyAndOrderFront(nil)
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }

    func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
        return true
    }
}