Detecting when a space changes in Spaces in Mac OS X

て烟熏妆下的殇ゞ 提交于 2019-11-28 04:35:24
BendiLow

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(), &currentSpace);

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!