Created
November 29, 2025 18:23
-
-
Save faganello60/72126412aa8645821a18e7364d103031 to your computer and use it in GitHub Desktop.
Code that listens for changes on screens in Swift.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class EventObserverDemo { | |
| var lastCount = NSScreen.screens.count | |
| init() { | |
| NotificationCenter.default.addObserver( | |
| self, | |
| selector: #selector(trigger), | |
| name: NSApplication.didChangeScreenParametersNotification, | |
| object: nil | |
| ) | |
| } | |
| @objc private func trigger(notification: NSNotification) { | |
| let newCount = NSScreen.screens.count | |
| if newCount != lastCount { | |
| print("Switched from \(lastCount) to \(newCount) displays") | |
| lastCount = newCount | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment