屏幕保护程序启动时是否触发了事件?就像钥匙串锁定一样:
OSStatus keychain_locked(SecKeychainEvent keychainEvent, SecKeychainCallbackInfo *info, void *context){...}
屏幕保护程序启动时是否触发了事件?就像钥匙串锁定一样:
OSStatus keychain_locked(SecKeychainEvent keychainEvent, SecKeychainCallbackInfo *info, void *context){...}
终于找到了——解决方案是使用 NSDistributedNotificationCenter 并观察以下事件
像
[[NSDistributedNotificationCenter defaultCenter]
addObserver:self
selector:@selector(screensaverStarted:)
name:@"com.apple.screensaver.didstart"
object:nil];
虽然没有为此的 Carbon 事件,但您可以在当前应用程序更改时收到通知,然后检查新应用程序是否为屏幕保护程序进程。
// Register the event handler for when applications change
{
EventTypeSpec es;
es.eventClass = kEventClassApplication;
es.eventKind = kEventAppFrontSwitched;
InstallApplicationEventHandler(&appChanged, 1, &es, NULL, NULL);
}
OSStatus appChanged(EventHandlerCallRef nextHandler, EventRef anEvent, void* userData)
{
ProcessSerialNumber psn;
GetEventParameter(anEvent, kEventParamProcessID, typeProcessSerialNumber,
NULL, sizeof(psn), NULL, &psn);
// Determine process name
char procName[255];
{
ProcessInfoRec pInfo;
Str255 procName255;
FSRef ref;
pInfo.processInfoLength = sizeof(ProcessInfoRec);
pInfo.processName = procName255;
pInfo.processAppRef = &ref;
GetProcessInformation(&psn, &pInfo);
const unsigned int size = (unsigned int)procName255[0];
memcpy(procName, procName255 + 1, size);
procName[size] = '\0';
}
if(strcmp(procName, "ScreenSaverEngine") == 0)
{
NSLog(@"Found %s\n", procName);
}
return noErr;
}
这不完全是问题的答案,但我花了很多时间徒劳地寻找 macOS 发布的通知列表,所以我想发布一些我为通知发现编写的代码。
该代码只需注册即可收听所有通知,并在收到通知时为每个通知打印一些信息。
import Foundation
let distCenter = CFNotificationCenterGetDistributedCenter()
if distCenter == nil {
exit(1)
}
CFNotificationCenterAddObserver(distCenter, nil, { (center, observer, name, object, userInfo) -> Void in
print("Event occurred: \(name) User info: \(userInfo)")
}, nil, nil, .DeliverImmediately)
CFRunLoopRun()