Skip to content

Instantly share code, notes, and snippets.

@faganello60
Created November 29, 2025 18:23
Show Gist options
  • Select an option

  • Save faganello60/72126412aa8645821a18e7364d103031 to your computer and use it in GitHub Desktop.

Select an option

Save faganello60/72126412aa8645821a18e7364d103031 to your computer and use it in GitHub Desktop.
Code that listens for changes on screens in Swift.
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