How to listen for when an external display connects/disconnects?


#1

Hey there! Is there a way to listen when an external display is connected or disconnected? Possibly via systemPreferences.subscribeNotification?

Thanks! :slight_smile:


#2

Update after some digging:

It looks like I want to listen for NSApplicationDidChangeScreenParametersNotification events sent by the default notification center [NSNotificationCenter defaultCenter].

Is this possible with Electron? Yep :point_down:


#3

The following does invoke the callback when I connect or disconnect an external display, however it doesn’t exactly tell me if the display was connected or disconnected… progress.

systemPreferences.subscribeLocalNotification('NSApplicationDidChangeScreenParametersNotification', (...args) => {
   console.log(args);
   // => [ 'NSApplicationDidChangeScreenParametersNotification', {} ]
});

I don’t suppose there’s a pure JS/Electron way to get the number of active screens?


#4

Oh. Duh. Everything I’ve needed to do thusfar is covered in the native screen module.

rtfm