How to Check if Your Screen is Locked in Python?

Estimated read time 3 min read

In Python, you can use platform-specific libraries or system commands to check if your screen is locked. Here are two approaches you can take:

  1. Using the pyautogui library (cross-platform):

The pyautogui library provides functions to interact with the screen. You can use the pyautogui.onScreenLock event to check if the screen is locked. Here’s an example:

import pyautogui

def check_screen_lock():
    screen_locked = pyautogui.onScreenLock()
    if screen_locked:
        print("Screen is locked.")
    else:
        print("Screen is not locked.")

# Example usage
check_screen_lock()
  1. Using system commands (platform-specific):

You can use system commands to check the screen lock status by executing them from Python using the subprocess module. Here’s an example for different operating systems:

For Windows:

import subprocess

def check_screen_lock():
    cmd = 'powershell -command "(Add-Type -TypeDefinition \\"using System;\\"
           + \\"using System.Runtime.InteropServices;\\"
           + \\"public class LockDetect{\\"
           + \\"[DllImport(\\\\"user32.dll\\\\")]\\"
           + \\"public static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);\\"
           + \\"[StructLayout(LayoutKind.Sequential)]\\"
           + \\"public struct LASTINPUTINFO{\\"
           + \\"public uint cbSize;\\"
           + \\"public int dwTime;\\"
           + \\"}\\"
           + \\"public static int GetIdleTime(){\\"
           + \\"LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();\\"
           + \\"lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);\\"
           + \\"GetLastInputInfo(ref lastInputInfo);\\"
           + \\"return (Environment.TickCount - lastInputInfo.dwTime);\\"
           + \\"}\\"
           + \\"}\\"
           + \\"public class Program{\\"
           + \\"public static void Main(){\\"
           + \\"LockDetect lockDetect = new LockDetect();\\"
           + \\"Console.WriteLine(lockDetect.GetIdleTime());\\"
           + \\"}\\"
           + \\"}\\" | Out-String -Stream | Invoke-Expression"'
    result = subprocess.check_output(cmd, shell=True)
    
    idle_time = int(result.strip())
    if idle_time > 0:
        print("Screen is locked.")
    else:
        print("Screen is not locked.")

# Example usage
check_screen_lock()

For macOS:

import subprocess

def check_screen_lock():
    cmd = 'ioreg -r -k IOHIDSecureEventInput | grep -i SecureEventTimestamp | tail -n1'
    result = subprocess.check_output(cmd, shell=True)
    
    secure_event_timestamp = int(result.decode().strip().split()[-1])
    if secure_event_timestamp > 0:
        print("Screen is locked.")
    else:
        print("Screen is not locked.")

# Example usage
check_screen_lock()

For Linux:

import subprocess

def check_screen_lock():
    cmd = 'gnome-screensaver-command -q | grep -i "is active"'
    result = subprocess.check_output(cmd, shell=True)
    
    if "is active" in result.decode():
        print("Screen is locked.")
    else:
        print("Screen is not locked.")

# Example usage
check_screen_lock()

These approaches use different methods depending on the operating system. The first approach using `pyautogui` is cross-platform, but it may not work on some systems if the necessary dependencies are not installed. The second approach using system commands is platform-specific and relies on external commands available on the respective operating system.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply