Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the internal macbook screen with NSScreen

If I have an external monitor connected to my MacBook, how would I retrieve the MacBook screen?

Either of the screens could be the screen with the menubar and the dock. They could also have the same resolution, the same name etc..

Is it posible to determine it without requesting the user to unplug all screens except the MacBook screen?

like image 301
Tyilo Avatar asked May 06 '12 09:05

Tyilo


1 Answers

You can use CGDisplayIsBuiltin() to find out if the display is builtin.

Example code:

int i = 0;
for(NSScreen* screen in [NSScreen screens]) {
    NSDictionary* screenDictionary = [screen deviceDescription];
    NSNumber* screenID = [screenDictionary objectForKey:@"NSScreenNumber"];
    CGDirectDisplayID aID = [screenID unsignedIntValue];     
    NSLog(@"Screen number %i is%@ builtin", i, CGDisplayIsBuiltin(aID)? @"": @" not");
    i++;
}
like image 150
Tyilo Avatar answered Oct 01 '22 11:10

Tyilo