如何在 Cocoa 中以编程方式设置屏幕旋转配置?

Gil*_*les 5 macos cocoa screen-orientation

在系统偏好设置 -> OS X 上的显示中,可以设置连接屏幕的旋转。

无论如何我可以以编程方式设置这个值吗?

我可以使用 CGDisplayRotation 获取当前设置。我似乎能够在一个简单的事务中设置许多屏幕属性,例如分辨率:

CGDisplayConfigRef config;
CGError error = CGBeginDisplayConfiguration(&config);
...
error = CGCompleteDisplayConfiguration(config, kCGConfigurePermanently);
Run Code Online (Sandbox Code Playgroud)

...但无论如何我都找不到设置此特定属性的方法。

有没有人知道这样做的方法?

小智 3

这可以通过未记录的方式来完成。示例将显示方向设置为 90 度:

CGDirectDisplayID display = CGMainDisplayID();
io_service_t service = CGDisplayIOServicePort(display);
IOOptionBits options = (0x00000400 | (kIOScaleRotate90)  << 16);
IOServiceRequestProbe(service, options);
Run Code Online (Sandbox Code Playgroud)

常量在 IOKit.framework IOGraphicsTypes.h 中定义

  • 谢谢,但这对我不起作用......并且 IOSServiceRequestProbe 在 Mavericks 中已被弃用,没有替代品。 (2认同)