Issue #764

Use a custom KeyAwareView that uses an NSView that checks for keyDown method. In case we can’t handle certain keys, call super.keyDown(with: event)

import SwiftUI
import KeyboardShortcuts

struct KeyAwareView: NSViewRepresentable {
    let onEvent: (Event) -> Void

    func makeNSView(context: Context) -> NSView {
        let view = KeyView()
        view.onEvent = onEvent
        DispatchQueue.main.async {
            view.window?.makeFirstResponder(view)
        }
        return view
    }

    func updateNSView(_ nsView: NSView, context: Context) {}
}

extension KeyAwareView {
    enum Event {
        case upArrow
        case downArrow
        case leftArrow
        case rightArrow
        case space
        case delete
        case cmdC
    }
}

private class KeyView: NSView {
    var onEvent: (KeyAwareView.Event) -> Void = { _ in }

    override var acceptsFirstResponder: Bool { true }
    override func keyDown(with event: NSEvent) {
        switch Int(event.keyCode) {
        case KeyboardShortcuts.Key.delete.rawValue:
            onEvent(.delete)
        case KeyboardShortcuts.Key.upArrow.rawValue:
            onEvent(.upArrow)
        case KeyboardShortcuts.Key.downArrow.rawValue:
            onEvent(.downArrow)
        case KeyboardShortcuts.Key.leftArrow.rawValue:
            onEvent(.leftArrow)
        case KeyboardShortcuts.Key.rightArrow.rawValue:
            onEvent(.rightArrow)
        case KeyboardShortcuts.Key.space.rawValue:
            onEvent(.space)
        case KeyboardShortcuts.Key.c.rawValue where event.modifierFlags.contains(.command):
            onEvent(.cmdC)
        default:
            super.keyDown(with: event)
        }
    }
}

Then we can place this as a background

LazyVStack {

}
.background(KeyAwareView(onEvent: {}))