检测 Mac OS X 中的空间中的空间何时发生变化

发布于 2024-08-24 17:10:23 字数 353 浏览 6 评论 0原文

假设我想编写一个简单的 Cocoa 应用程序来使 Leopard 的 Spaces 功能更加有用。我想将每个空间配置为具有不同的

  • 屏幕分辨率
  • 键盘布局
  • 音量(用于音频)

所以我的问题有两个部分:

  1. 我想有办法独立于空间来修改这三件事,对吧?如果是这样,怎么办?
  2. 如何在我的应用程序中检测何时发生空间更改,并在发生这种情况时确定用户刚刚切换到哪个空间? Leopard 会发出一些分布式通知之类的吗?

更新:从 Mac App Store 上所有与 Spaces 相关的应用程序来看,必须有一些公共 API 方法来执行此操作。

Let's say I want to write a simple Cocoa app to make the Spaces feature of Leopard more useful. I would like to configure each space to have, say, different

  • screen resolutions
  • keyboard layouts
  • volume (for audio)

So there are two parts to my question:

  1. I suppose there are ways to modify these three things independently of Spaces, right? If so, how?
  2. How can I detect in my app when a space change occurs, and when that happens, determine what space the user just switched to? Does Leopard send out some distributed notifications or something?

Update: There has to be some public API way of doing this, judging from all the Spaces-related apps on the Mac App Store.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白鸥掠海 2024-08-31 17:10:23

正如 Peter 所说,在 10.6 中,您可以使用 NSWorkSpace NSWorkspaceActiveSpaceDidChangeNotification 在工作区更改时获取通知。

然后,您可以使用 Quartz API 确定当前空间,kCGWindowWorkspace 字典键保存工作空间。
例如:

int currentSpace;
// get an array of all the windows in the current Space
CFArrayRef windowsInSpace = CGWindowListCopyWindowInfo(kCGWindowListOptionAll | kCGWindowListOptionOnScreenOnly, kCGNullWindowID);      

// now loop over the array looking for a window with the kCGWindowWorkspace key
for (NSMutableDictionary *thisWindow in (NSArray *)windowsInSpace)
{
     if ([thisWindow objectForKey:(id)kCGWindowWorkspace])
       {
           currentSpace = [thisWindow objectForKey(id)kCGWindowWorkspace] intValue];
           break;
       }
}

您也可以使用私有 API 获取空间,请查看 CGPSrivate.h 它允许您执行此操作:

int currentSpace = 0;
CGSGetWorkspace(_CGSDefaultConnection(), ¤tSpace);

要更改屏幕分辨率,您需要查看 Quartz 服务,用于更改音量这可能有帮助

As Peter says, in 10.6 you can use the NSWorkSpace NSWorkspaceActiveSpaceDidChangeNotification to get a notification when the workspace changes.

You can then determine the current space using Quartz API, the kCGWindowWorkspace dictionary key holds the workspace.
e.g:

int currentSpace;
// get an array of all the windows in the current Space
CFArrayRef windowsInSpace = CGWindowListCopyWindowInfo(kCGWindowListOptionAll | kCGWindowListOptionOnScreenOnly, kCGNullWindowID);      

// now loop over the array looking for a window with the kCGWindowWorkspace key
for (NSMutableDictionary *thisWindow in (NSArray *)windowsInSpace)
{
     if ([thisWindow objectForKey:(id)kCGWindowWorkspace])
       {
           currentSpace = [thisWindow objectForKey(id)kCGWindowWorkspace] intValue];
           break;
       }
}

Alternatively you can get the Space using the private API, take a look at CGSPrivate.h which allows you to do this:

int currentSpace = 0;
CGSGetWorkspace(_CGSDefaultConnection(), ¤tSpace);

To change the screen resolution you'll want to look at Quartz services, for altering the volume this may be helpful.

扭转时空 2024-08-31 17:10:23

NSWorkspace 在其自己的通知中心发布 NSWorkspaceActiveSpaceDidChangeNotification,但仅限于 Snow Leopard。

NSWorkspace posts a NSWorkspaceActiveSpaceDidChangeNotification on its own notification center, but only on Snow Leopard.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文