以编程方式触发“检测显示". [英] Programmatically trigger "detect displays."
问题描述
我正在尝试触发与您单击检测显示"时系统所做的相同的操作.在展示"中系统偏好设置窗格.有没有办法以编程方式执行此操作?我的目标是创建一个LaunchAgent,在登录时执行此操作,以重置显示分辨率,以防用户弄乱它.
I’m trying to trigger the same thing that the system does when you click on “Detect Displays” in the “Displays” System Preferences pane. Is there a way to do this programmatically? My goal is to create a LaunchAgent that does this at login to reset the display resolution in case a user messes with it.
推荐答案
您将需要使用私有的CoreGraphics例程来获取所有显示(包括非活动显示)的列表,然后请求重新扫描总线.像这样尝试:
You will need to use a private CoreGraphics routine to get the list of all displays including inactive ones, and then request a rescan of the bus. Try it like this:
#include <IOKit/IOKitLib.h>
#include <IOKit/IOTypes.h>
CGDisplayErr CGSGetDisplayList(CGDisplayCount maxDisplays,
CGDirectDisplayID * onlineDspys,
CGDisplayCount * dspyCnt);
static void DetectDisplays()
{
CGDirectDisplayID displays[8];
CGDisplayCount dspCount = 0;
if (CGSGetDisplayList(8, displays, &dspCount) == noErr)
{
for(int i = 0; i < dspCount; i++)
{
io_service_t service = CGDisplayIOServicePort(displays[i]);
if (service)
IOServiceRequestProbe(service, kIOFBUserRequestProbe);
}
}
}
并链接到ApplicationServices和IOKit.
And link to ApplicationServices and IOKit.
这篇关于以编程方式触发“检测显示".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!