I’m building a MenuBarExtra app that allows users to paste items into other apps from a context menu. I need to detect when the context menu has lost focus (keyWindow status) or another app has gained key status before I initiate the paste. Using NSWindow.didResignKeyNotification
works when debugging in Xcode but does not work when the app is published as the notification arrives before the other app has gained focus.
Other notifications like NSWorkspace.didActivateApplicationNotification
do not seem to fire for MenuBarExtra apps at all.
The only solution that has worked is to set a delay timer before doing the paste, but this is hacky, introduces an unnecessary delay, and may fail on slower systems. Is there another notification I can use? Any other suggestions?