There are several ways to achieve this:
You can implement applicationDidChangeScreenParameters: in your application deletion (this method is part of NSApplicationDelegateProtocol ).
Another way is to listen for the NSApplicationDidChangeScreenParametersNotification sent by the default notification center [NSNotificationCenter defaultCenter] .
Whenever you call the delegate method or get a notification, you can [NSScreen screens] over [NSScreen screens] and see if the display is connected or removed (you need to save the display list, which you can check when the program starts).
The approach without Cocoa will be through graphic display services:
You must implement the reconfiguration function and register it with CGDisplayRegisterReconfigurationCallback(CGDisplayReconfigurationCallBack cb, void* obj);
In your reconfiguration function, you can query the status of the affected display. For example:.
void DisplayReconfigurationCallBack(CGDirectDisplayID display, CGDisplayChangeSummaryFlags flags, void* userInfo) { if(display == someDisplayYouAreInterestedIn) { if(flags & kCGDisplaySetModeFlag) { ... } if(flags & kCGDisplayRemoveFlag) { ... } if(flags & kCGDisplayDisabledFlag) { ... } } if(flags & kCGDisplaySetModeFlag || flags & kCGDisplayDisabledFlag || flags & kCGDisplayRemoveFlag) { ... } }
Thomas zoechling
source share