Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

pgbhagat

macrumors newbie
Original poster
Jun 26, 2009
4
0
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

Moderator emeritus
Jun 15, 2000
7,958
7
What do you mean by locked? Logged out? Screen saver on with password? ???
 

ceharris

macrumors newbie
Jul 3, 2009
1
0
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.

Code:
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

macrumors newbie
Original poster
Jun 26, 2009
4
0
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:mad:selector(screenIsLocked:) name:mad:"com.apple.screenIsLocked" object:nil];

[center
addObserver:screen
selector:mad:selector(screenIsUnlocked:)
name:mad:"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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

macrumors newbie
Original poster
Jun 26, 2009
4
0
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

macrumors newbie
Original poster
Jun 26, 2009
4
0
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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.