Key windows in OS X development

Viewed 103

Issue

I'm developing a software for MacOS where the application window is displayed at the top when the shortcut key is pressed,

but after close the window, there is no key window on screen.I must click someone to make it as key window. I hope my app could work like this:

  1. there is A application window is key window.
  2. I press shortcut key and my app window show at the top
  3. I close my app by shortcut key, and the A application window become key window again.

Context

this is my code

let hostViewController = NSHostingController(rootView: ContentView())
self.mainWindow = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 200, height: 200), styleMask: [.titled,.borderless,.closable,.miniaturizable], backing: .buffered, defer: false)
self.mainWindow.collectionBehavior = [.canJoinAllSpaces,]
self.mainWindow.isMovable = false
self.mainWindow.title  = "test"
self.mainWindow.hidesOnDeactivate = true
self.mainWindow.order(.above, relativeTo: 0)
self.mainWindow.canHide = true
self.mainWindow.hidesOnDeactivate = false
self.mainWindow.contentView = hostViewController.view
self.mainWindow.orderFront(nil)

What I've tried

  1. Replace NSWindow with NSPanel
  2. Set isFloatingPanel = true

but all failed. I search apple document about nswindow but look like nothing can help me.

Maybe there is no API to implement this requirement directly, so is there another way to implement it?

0 Answers
Related