1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

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

Discussion in 'Mac Programming' started by pgbhagat, Jun 26, 2009.

  1. macrumors newbie

    #1
    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 :)
     
  2. Moderator emeritus

    kainjow

    #2
    What do you mean by locked? Logged out? Screen saver on with password? ???
     
  3. macrumors newbie

    #3
    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.
     
  4. macrumors newbie

    #4
    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.
     
  5. Moderator

    robbieduncan

    Staff Member

    #5
    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.
     
  6. macrumors newbie

    #6
    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
     
  7. macrumors newbie

    #7
    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?
     

Share This Page