Get NSScreen with keyboard focus on the sandbox

What I want to know is that NSScreen has keyboard focus.

First I added an observer for NSWorkspace.didActiveApplicationNotification and whenever the foo method is called, I need to determine which NSScreen has keyboard focus.

 NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(foo), name: NSWorkspace.didActivateApplicationNotification, object: nil) 

All I received from this notification is the NSRunningApplication object, but it does not tell me which screen is focused.

In the NSScreen Documentation, I found the main NSScreen property with a description

Returns a display object containing a window using the keyboard focus.

This is what I want, but it does not work as I thought. It returns the wrong screen. I found this information in the header file

// cached value, possibly outdated

I tried accessibility, everything was fine, but it does not work when Sandbox is turned on, so I can not use it.

Does anyone know how to get an NSScreen object with keyboard focus?

+8
accessibility cocoa swift macos appstore-sandbox
source share

No one has answered this question yet.

See related questions:

762
Get string length
95
How to enable keyboard focus links in Firefox?
twenty
How to get home directory of users in objective-c in an isolated application?
7
| NSScreen mainScreen | not updated when keyboard / mouse focus changes to another monitor?
6
Getting the main application window using an NSRunningApplication instance
5
UIScreen.mainScreen.scale on NSScreen
2
ViewController deinit after running segue using notification
2
NSNotification for one object in fast
one
Correctly determine the appearance and disappearance of the keyboard
0
Determine when NSWindow closes compared to leaving full screen

All Articles