How does UIScene know whether it has the main key window and when it changes on iPad's multi-scene?

Hi.

I know to know which window gets hardware keyboard events (such as shortcut key) currently on iPad.

Until iPadOS 15.0, UIApplication.shared.keyWindow, which was deprecated on iPadOS 13.0 and didBecomeKeyNotification/didResignKeyNotification.

But after iPadOS 15.0, a keyWindow is managed by UIScene, not by UIApplication.

Each scene of my app always has just one window. For my purpose, checking deprecated UIApplication.shared.keyWindow is still effective but didBecomeKeyNotification and didResignKeyNotification don't work because they are fired when a change happens only inside the scene.

So my questions are,

  1. What is the new alternative of UIApplication.shared.keyWindow?

I know a wrong hack like

    UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first?.windows.filter { $0.isKeyWindow }.first

does not work since the order of connectedScenes is not related with getting hardware keyboard events.

  1. What are the new alternatives of didBecomeKeyNotification/didResignKeyNotification which work on inter-scene?

The second question is more crucial. Because about the first question, I can still use deprecated UIApplication.shared.keyWindow.

Thanks.

How does UIScene know whether it has the main key window and when it changes on iPad's multi-scene?
 
 
Q