2 years ago

#72054

test-img

Nathan Z

SwiftUI/MacOS - Keyboard Shortcut in new window

I'm currently programming a MacOS app with SwiftUI. Thanks to a view extension function, i'm creating a new window. And in that window i'm trying to perform an action with a .keyboardShortcut method. Everything is working fine on an Apple Silicon Mac (actually mine) but on an Intel processor Mac, the keyboard shortcut doesn't want to be considered. I'm trying to figure out if the issue comes from the keyboard shortcut itself or maybe the of i'm creating my window.. but i can't really understand where i could be wrong, especially when it works on Apple Silicon...

The func to create a new view

extension View {
    
    func openInWindow(title: String, sender: Any?) -> NSWindow {
        let controller = NSHostingController(rootView: self)
        let win = NSWindow(contentViewController: controller)
        win.contentViewController = controller
        win.title = title
        win.makeKeyAndOrderFront(sender)
        return win
    }
    
}

And my code where i call the .keyboardShortcut method

VStack(spacing: 15) {
    Button(action: {
        newTaskVM.addTask()
        NSApplication.shared.keyWindow?.close()
        boardService.isTaskCreated = true
    }) {}
    .keyboardShortcut(.defaultAction)
    .padding(0)
    .opacity(0)
    .frame(width: 0, height: 0)
}

xcode

macos

swiftui

keyboard

shortcut

0 Answers

Your Answer

Accepted video resources