显示器连接或断开时发出通知 [英] Notification when display gets connected or disconnected

查看:194
本文介绍了显示器连接或断开时发出通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OS X应用程序,该应用程序在所有连接的显示器的所有可用空间上显示自定义窗口。
我可以通过调用[NSScreen屏幕]获得可用显示对象的数组。

I'm working on an OS X application that displays custom windows on all available spaces of all the connected displays. I can get an array of the available display objects by calling [NSScreen screens].

我目前所缺少的是一种告诉用户是否

What I'm currently missing is a way of telling if the user connects a display to or disconnects a screen from their system.

我已经在Cocoa文档中搜索了一些通知,这些通知处理的是这种情况,但运气不佳,我拒绝相信更改连接到系统的显示器数量时不会发布任何系统通知。

I have searched the Cocoa documentation for notifications that deal with a scenario like that without much luck, and I refuse to believe that there isn't some sort of system notification that gets posted when changing the number of displays connected to the system.

关于如何解决此问题的任何建议?

Any suggestions on how to solve this problem?

推荐答案

有几种方法可以实现:

您可以实现 applicationDidChangeScreenParameters:在您的应用程序委托中(方法是 NSApplicationDelegateProtocol )的一部分。

另一种方法是监听 NSApplicationDidChangeScreenParametersNotification 默认通知中心 [NSNotificationCenter defaultCenter]

There are several ways to achieve that:
You could implement applicationDidChangeScreenParameters: in your app delegate (the method is part of the NSApplicationDelegateProtocol).
Another way is to listen for the NSApplicationDidChangeScreenParametersNotification sent by the default notification center [NSNotificationCenter defaultCenter].

无论何时调用委托方法或收到通知,都可以遍历 [NSScreen屏幕] 并查看如果显示器已连接或卸下(您必须维护一个显示器列表,可以在程序启动时进行检查)。

Whenever your delegate method is called or you receive the notification, you can iterate over [NSScreen screens] and see if a display got connected or removed (you have to maintain a display list you can check against at program launch).

非可可方法是通过Core Graphics Display服务:

您必须实现重新配置功能并向 CGDisplayRegisterReconfigurationCallback(CGDisplayReconfigurationCallBack cb,void * obj);

A non-Cocoa approach would be via Core Graphics Display services:
You have to implement a reconfiguration function and register it with CGDisplayRegisterReconfigurationCallback(CGDisplayReconfigurationCallBack cb, void* obj);

在重新配置功能中,您可以查询受影响的显示器的状态。例如:

In your reconfiguration function you can query the state of the affected display. E.g.:

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)
    {
        ...
    }
}

这篇关于显示器连接或断开时发出通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆