Lock screen by API in macOS

后端 未结 8 1867
梦如初夏
梦如初夏 2020-12-12 16:49

Is there an API, that can lock the screen as the menu bar entry you can add from Keychain preferences?

This Keychain function is (was) lock

8条回答
  •  离开以前
    2020-12-12 17:24

    To lock computer and show password promt for recent user you can use this code:

    - (void)lockScreen
    {
        MDSendAppleEventToSystemProcess(kAESleep);
    }
    
    OSStatus MDSendAppleEventToSystemProcess(AEEventID eventToSendID)
    {
        AEAddressDesc                    targetDesc;
        static const ProcessSerialNumber kPSNOfSystemProcess = {0, kSystemProcess};
        AppleEvent                       eventReply          = {typeNull, NULL};
        AppleEvent                       eventToSend         = {typeNull, NULL};
    
        OSStatus status = AECreateDesc(typeProcessSerialNumber,
                &kPSNOfSystemProcess, sizeof(kPSNOfSystemProcess), &targetDesc);
    
        if ( status != noErr ) return status;
    
        status = AECreateAppleEvent(kCoreEventClass, eventToSendID,
                &targetDesc, kAutoGenerateReturnID, kAnyTransactionID, &eventToSend);
    
        AEDisposeDesc(&targetDesc);
    
        if ( status != noErr ) return status;
    
        status = AESendMessage(&eventToSend, &eventReply,
                kAENormalPriority, kAEDefaultTimeout);
    
        AEDisposeDesc(&eventToSend);
        if ( status != noErr ) return status;
        AEDisposeDesc(&eventReply);
        return status;
    }
    

提交回复
热议问题