PDA

View Full Version : How to know programmatically if a Mac Machine is screen locked?




pgbhagat
Jun 26, 2009, 07:43 AM
I am desperately finding the solution for the following problem;



How to know programmatically if a Mac Machine is screen locked?

How do I tell my Application that the Mac machine is screen locked/unlocked?





1. I have my Application which runs on a Mac machine,

2. App should behave differently depending on whether the machine is locked or unlocked.

3. if Mac is screen lock, then execute PATH1,

4. else execute PATH2,



Is there any command on Mac which will tell me the whether the current machine is locked or unlocked?



Or



Is there any event that gets dispatched when user locks the machine? What is that event?





Thanks in advance,

Prashant :)



kainjow
Jun 26, 2009, 05:10 PM
What do you mean by locked? Logged out? Screen saver on with password? ???

ceharris
Jul 3, 2009, 03:51 AM
The following notifications are posted to the default distributed notification center for screen lock and screen saver events:

com.apple.screenIsLocked
com.apple.screenIsUnlocked

com.apple.screensaver.didstart
com.apple.screensaver.willstop
com.apple.screensaver.didstop

I haven't found declarations for these events in a header anywhere, and I'm not certain that they're part of any published API, but the events are observable, at least in Leopard.

Observing screen lock/unlock is then a simple a matter of adding yourself as an observer for the screenIsLocked and screenIsUnlocked event. In Cocoa, you'd do something like this to register for these events.


NSDistributedNotificationCenter * center =
[NSDistributedNotificationCenter defaultCenter];

[center
addObserver:self
selector:@selector(screenIsLocked:)
name:@"com.apple.screenIsLocked"
object:nil];

[center
addObserver:self
selector:@selector(screenIsUnlocked:)
name:@"com.apple.screenIsUnlocked"
object:nil];



In your case, it sounds like the callback methods (screenIsLocked:, screenIsUnlocked: ) would simply set a flag that would be used to determine which path to follow in the code.

Of course, you'll want to remove yourself as an observer of the distributed center before your app terminates.

pgbhagat
Sep 22, 2009, 07:25 AM
Thank you ceharris.

But i trying to catch mentioned notification but program does not responds when the screen lock or screen saver start.


here are the details;

main.m
======
#import <Cocoa/Cocoa.h>
#include "ScreenLock.h"

int main(int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ScreenLock *screen = [[ScreenLock alloc] init];

NSDistributedNotificationCenter * center =
[NSDistributedNotificationCenter defaultCenter];

[center addObserver:screen selector:@selector(screenIsLocked:) name:@"com.apple.screenIsLocked" object:nil];

[center
addObserver:screen
selector:@selector(screenIsUnlocked:)
name:@"com.apple.screenIsUnlocked"
object:nil];

=============
ScreenLock.m
=============


#import "ScreenLock.h"

@implementation ScreenLock

-(void)screenIsLocked
{
printf("Screen locked");
}
- (void) screenIsUnlocked
{
printf("Screen is unlocked");
}


Can you please provide this example in detail?
This will help me a lot.

robbieduncan
Sep 22, 2009, 07:43 AM
Your observers will never get called as the object you tell the notification centre to send notifications to is nil. You need to provide a valid instance of the class ScreenLock to get called. This is the same for all notifications.

pgbhagat
Sep 22, 2009, 08:20 AM
Using notifications,
I am able to capture following notification:

com.apple.screensaver.didstart
com.apple.screensaver.willstop
com.apple.screensaver.didstop

But following was not capture:
com.apple.screenIsLocked
com.apple.screenIsUnlocked

pgbhagat
Sep 22, 2009, 08:35 AM
Ok Fine..
In leopard, i am able to get all these notifications;
com.apple.screensaver.didstart
com.apple.screensaver.willstop
com.apple.screensaver.didstop
com.apple.screenIsLocked
com.apple.screenIsUnlocked

But in Tiger, following are not getting notified;
com.apple.screenIsLocked
com.apple.screenIsUnlocked

Any other way to get 'screenIsLocked', 'screenIsUnlocked' notifications in Tiger?