# In macOS how do you detect if my program window is selected or no longer selected on the screen?

**URL:** <https://forums.swift.org/t/in-macos-how-do-you-detect-if-my-program-window-is-selected-or-no-longer-selected-on-the-screen/53336>\
**Category:** Using Swift\
**Tags:** off-topic, swiftui\
**Created:** [November 7, 2021, 12:55am UTC](https://forums.swift.org/t/in-macos-how-do-you-detect-if-my-program-window-is-selected-or-no-longer-selected-on-the-screen/53336 "2021-11-07T00:55:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@Chris](https://forums.swift.org/u/Chris)\
**Post date:** [November 7, 2021, 12:55am UTC](https://forums.swift.org/t/in-macos-how-do-you-detect-if-my-program-window-is-selected-or-no-longer-selected-on-the-screen/53336/1 "2021-11-07T00:55:13Z")

</div>

Hello,  
I am trying to detect when my running swift program window is selected and not selected. The not selected case example would be Safari is now active and at the forefront but my swift program window is still on the screen just gray. I have looked at focus but it appears to apply to which element in a scene has focus, such as a text box. I also looked at active/inactive/background scene phases but in the example above (ie bringing Safari to the forefront) the scene phase does not appear to change in my test program. Any assistance in pointing me in the right direction will be appreciated.  
Below is the code I used to test active/inactive/background.  
Chris

```swift
struct MyView: View {
    @Environment(\.scenePhase) var scenePhase
    var body: some View {
        Text("Testing")
            .padding()
            .frame(width: 600, height: 400, alignment: .center)
            .onChange(of: scenePhase, perform: { phase in
                switch phase {
                case .active: print("Active")
                case .background: print("Background")
                case .inactive: print("Inactive")
                default: print("Who Knows")
                }
            })
    }
}

```

---

<div class="post-metadata">

**Author:** ![Peter-Schorn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/peter-schorn/32/11183_2.png) [@Peter-Schorn](https://forums.swift.org/u/Peter-Schorn)\
**Post date:** [November 7, 2021, 5:47am UTC](https://forums.swift.org/t/in-macos-how-do-you-detect-if-my-program-window-is-selected-or-no-longer-selected-on-the-screen/53336/2 "2021-11-07T05:47:10Z")

</div>

Use the `isKeyWindow` property:

[https://developer.apple.com/documentation/appkit/nswindow/1419735-iskeywindow](https://developer.apple.com/documentation/appkit/nswindow/1419735-iskeywindow)

---

<div class="post-metadata">

**Author:** ![Chris](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@Chris](https://forums.swift.org/u/Chris)\
**Post date:** [November 7, 2021, 11:06pm UTC](https://forums.swift.org/t/in-macos-how-do-you-detect-if-my-program-window-is-selected-or-no-longer-selected-on-the-screen/53336/3 "2021-11-07T23:06:38Z")

</div>

Peter thank you. This was the key. I had some trouble understanding how to implement it but found this site which was very helpful. [https://lostmoa.com/blog/ReadingTheCurrentWindowInANewSwiftUILifecycleApp/](https://lostmoa.com/blog/ReadingTheCurrentWindowInANewSwiftUILifecycleApp/). Now when I select another window the isKeyWindow variable (a boolean) changes to false and back to true when I select the program window again. Perfect.

---

<div class="post-metadata">

**Author:** ![nickromano](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nickromano/32/9366_2.png) [@nickromano](https://forums.swift.org/u/nickromano)\
**Post date:** [March 22, 2024, 5:05pm UTC](https://forums.swift.org/t/in-macos-how-do-you-detect-if-my-program-window-is-selected-or-no-longer-selected-on-the-screen/53336/4 "2024-03-22T17:05:24Z")

</div>

From this article it looks like we can use the `controlActiveState` environment for mac - [Detect focused window on macOS](https://nilcoalescing.com/blog/DetectFocusedWindowOnMacOS/)

```swift
@Environment(\.controlActiveState) var controlActiveState

controlActiveState == .key // window became active
controlActiveState == .inactive // window became inactive

```
