Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 26, 2009, 07:43 AM   #1
pgbhagat
macrumors newbie
 
Join Date: Jun 2009
How to know programmatically if a Mac Machine is screen locked?

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
pgbhagat is offline   0 Reply With Quote
Old Jun 26, 2009, 05:10 PM   #2
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
What do you mean by locked? Logged out? Screen saver on with password? ???
kainjow is offline   0 Reply With Quote
Old Jul 3, 2009, 03:51 AM   #3
ceharris
macrumors newbie
 
Join Date: Jul 2009
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.
ceharris is offline   0 Reply With Quote
Old Sep 22, 2009, 07:25 AM   #4
pgbhagat
Thread Starter
macrumors newbie
 
Join Date: Jun 2009
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.
pgbhagat is offline   0 Reply With Quote
Old Sep 22, 2009, 07:43 AM   #5
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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.
robbieduncan is offline   0 Reply With Quote
Old Sep 22, 2009, 08:20 AM   #6
pgbhagat
Thread Starter
macrumors newbie
 
Join Date: Jun 2009
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 is offline   0 Reply With Quote
Old Sep 22, 2009, 08:35 AM   #7
pgbhagat
Thread Starter
macrumors newbie
 
Join Date: Jun 2009
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?
pgbhagat is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Time Machine locked my HDD guildmac Mac Pro 0 Mar 4, 2014 07:47 AM
Put back/delete locked items in Time-Machine Backups PajamaPants OS X Mavericks (10.9) 4 Dec 31, 2013 07:15 AM
iPhone 5% drop when screen is locked? joegleur iPhone Tips, Help and Troubleshooting 7 Feb 26, 2013 03:37 AM
audio notifications when screen is locked? jstar002 iPhone 0 Feb 11, 2013 08:30 AM
iMessage when screen is locked hugoleveille iOS 6 4 Oct 9, 2012 10:31 AM

Forum Jump

All times are GMT -5. The time now is 04:02 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps