目次
概要
実装
スリープ状態はNotificationCenterから取得することができる。
「コンピューターのスリープ」と「ディスプレイのスリープ」が別通知なので注意すること。この例では「コンピューターのスリープ」のみ対応しているが、現実的は両方を記述することになると思う。
コンピュータのスリープ突入 | NSWorkspace.WillSleepNotification |
コンピュータのスリープ復帰 | NSWorkspace.DidWakeNotification |
ディスプレイのスリープ突入 | NSWorkspace.ScreensDidSleepNotification |
ディスプレイのスリープ復帰 | NSWorkspace.ScreensDidWakeNotification |
using System;
using AppKit;
using Foundation;
namespace NotificationSleepWake
{
[Register("AppDelegate")]
public class AppDelegate : NSApplicationDelegate
{
public AppDelegate()
{
}
NSObject _wake;
public override void DidFinishLaunching(NSNotification notification)
{
// Insert code here to initialize your application
if (_wake == null)
{
_wake = NSWorkspace.SharedWorkspace.NotificationCenter.AddObserver(
NSWorkspace.DidWakeNotification,
(notify) =>
{
Console.WriteLine("wake");
});
}
}
public override void WillTerminate(NSNotification notification)
{
if (_wake != null)
{
NSWorkspace.SharedWorkspace.NotificationCenter.RemoveObserver(_wake);
}
}
}
}