<?xml version="1.0" encoding="UTF-8"?>        <rss version="2.0"
             xmlns:atom="http://www.w3.org/2005/Atom"
             xmlns:dc="http://purl.org/dc/elements/1.1/"
             xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
             xmlns:admin="http://webns.net/mvcb/"
             xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
             xmlns:content="http://purl.org/rss/1.0/modules/content/">
        <channel>
            <title>
									Lazarus Pascal - macOS - Determine if the screen is locked or not - Delphi, Lazarus, Free Pascal				            </title>
            <link>https://www.tweaking4all.com/forum/delphi-lazarus-free-pascal/lazarus-pascal-macos-determine-if-the-screen-is-locked-or-not/</link>
            <description>Tweaking4All.com Discussion Board</description>
            <language>en-US</language>
            <lastBuildDate>Mon, 16 Mar 2026 23:33:12 +0000</lastBuildDate>
            <generator>wpForo</generator>
            <ttl>60</ttl>
							                    <item>
                        <title>Lazarus Pascal - macOS - Determine if the screen is locked or not</title>
                        <link>https://www.tweaking4all.com/forum/delphi-lazarus-free-pascal/lazarus-pascal-macos-determine-if-the-screen-is-locked-or-not/#post-6233</link>
                        <pubDate>Fri, 30 Jan 2026 11:43:50 +0000</pubDate>
                        <description><![CDATA[Based on a user request, I had to figure out if a screen is locked or not.So with some tinkering (thank you StackExchange!) I found 2 ways to handle this.

Check if the screen is locked or...]]></description>
                        <content:encoded><![CDATA[<p>Based on a user request, I had to figure out if a screen is locked or not.<br />So with some tinkering (thank you StackExchange!) I found 2 ways to handle this.</p>
<ol>
<li>Check if the screen is locked or not with a simple one time call</li>
<li>With the system notifying my application that the lock state changed (so we do not have to poll if a screen is locked or not)</li>
</ol>
<p>So for this I made a little unit - feel free to use/modify/etc:</p>
<pre contenteditable="false">unit t4a_macOS_ScreenLocked;

{$mode ObjFPC}{$H+}
{$modeswitch objectivec1}

interface

uses
  Classes, SysUtils, MacOSAll, Forms, process, CocoaAll, CocoaUtils;


type
  TScreenLockUnlockCallback = procedure(isScreenLocked:boolean) of object;

{ T4ALIB - Check if the screen is locked or not (eg. Login screen shows). }
function CheckIfScreenLocked: Boolean;


{ Use Notifications by system}
procedure ScreenLockCallback(Center: CFNotificationCenterRef;
                             Observer: Pointer;
                             Name: CFStringRef;
                             Object_: Pointer;
                             UserInfo: CFDictionaryRef); mwpascal;
procedure RegisterScreenLockNotifications(OptionalCallbackProcedure:TScreenLockUnlockCallback = nil);
procedure UnregisterScreenLockNotifications;

var
  ScreenIsLocked    : boolean; // used for notifications
  CallbackProcedure : TScreenLockUnlockCallback;

implementation

// Onetime call/check
function CheckIfScreenLocked: Boolean;
var
  SessionDict: CFDictionaryRef;
  ValueRef: CFTypeRef;
begin
  Result := False;

  SessionDict := CGSessionCopyCurrentDictionary;
  if SessionDict &lt;&gt; nil then
  begin
    if CFDictionaryGetValueIfPresent(
         SessionDict,
         CFSTR('CGSSessionScreenIsLocked'),
         @ValueRef
       ) then
    begin
      if CFGetTypeID(ValueRef) = CFBooleanGetTypeID then
        Result := CFBooleanGetValue(CFBooleanRef(ValueRef));
    end;

    CFRelease(SessionDict);
  end;

  ScreenIsLocked := Result;
end;

// Notification by system
procedure ScreenLockCallback(Center: CFNotificationCenterRef;
                             Observer: Pointer;
                             Name: CFStringRef;
                             Object_: Pointer;
                             UserInfo: CFDictionaryRef); mwpascal;
var
  NotificationName: String;
begin
  NotificationName := CFStringToString(Name);

  ScreenIsLocked := (NotificationName =  'com.apple.screenIsLocked') and
                    (NotificationName &lt;&gt; 'com.apple.screenIsUnlocked');

  if Assigned(CallbackProcedure) then CallbackProcedure(ScreenIsLocked);
end;

procedure RegisterScreenLockNotifications(OptionalCallbackProcedure:TScreenLockUnlockCallback = nil);
var
  Center: CFNotificationCenterRef;
begin
  CallbackProcedure := OptionalCallbackProcedure;

  Center := CFNotificationCenterGetDistributedCenter;

  CFNotificationCenterAddObserver(
    Center,
    nil, // observer
    @ScreenLockCallback,
    CFSTR('com.apple.screenIsLocked'),
    nil,
    CFNotificationSuspensionBehaviorDeliverImmediately
  );

  CFNotificationCenterAddObserver(
    Center,
    nil,
    @ScreenLockCallback,
    CFSTR('com.apple.screenIsUnlocked'),
    nil,
    CFNotificationSuspensionBehaviorDeliverImmediately
  );
end;

procedure UnregisterScreenLockNotifications;
var
  Center: CFNotificationCenterRef;
begin
  Center := CFNotificationCenterGetDistributedCenter;
  CFNotificationCenterRemoveObserver(Center, nil, nil, nil);
end;

finalization

  UnregisterScreenLockNotifications;

end.</pre>
<p>&nbsp;</p>
<p>Use:</p>
<p>1. One time check:</p>
<p>Just call "<strong>CheckIfScreenLocked</strong>", which returns TRUE if the screen is locked.</p>
<p>2. Using notifications:</p>
<p>- define a <strong>public</strong> function that can be called when the screen locked state changes:</p>
<pre contenteditable="false">// your own procedure
procdure TForm1.ScreenLockChanged(locked:boolean);   { public! }
begin
  if locked the writeln('locked');
end;  </pre>
<p>- In the onCreate event of your form, activate the notification observer</p>
<pre contenteditable="false">procedure TForm1.FormCreate(Sender: TObject);
begin
   RegisterScreenLockNotifications(@ScreenLockChanged);
end;</pre>
<p>(the observer will be unregistered automatically when your application closes, or you can call "<strong>UnregisterScreenLockNotifications</strong>")</p>
<p>&nbsp;</p>
<p>Hope this is of use to someone out there &#x1f609; </p>]]></content:encoded>
						                            <category domain="https://www.tweaking4all.com/forum/delphi-lazarus-free-pascal/">Delphi, Lazarus, Free Pascal</category>                        <dc:creator>Hans</dc:creator>
                        <guid isPermaLink="true">https://www.tweaking4all.com/forum/delphi-lazarus-free-pascal/lazarus-pascal-macos-determine-if-the-screen-is-locked-or-not/#post-6233</guid>
                    </item>
							        </channel>
        </rss>
		