通过Python3在Mac上检测锁定屏幕 [英] Detecting lock screen on Mac through Python3

查看:284
本文介绍了通过Python3在Mac上检测锁定屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图找到一种使用Python3检测屏幕是否在Mac系统上锁定的方法

Trying to find a way to detect if the screen is locked on a Mac system, using Python3

我尝试了以下操作,它给出了KeyError:

I've tried the following, and it gives a KeyError:

import Quartz
gui_dict = Quartz.CGSessionCopyCurrentDictionary()
print(gui_dict['CGSSessionScreenIsLocked'])

import Quartz
all_windows = Quartz.CGWindowListCopyWindowInfo(Quartz.kCGWindowListOptionOnScreenOnly, Quartz.kCGNullWindowID)
for x in all_windows:
    if x["kCGWindowOwnerName"] == "loginwindow"
        print("Locked!")

此代码可在Python2上运行,但考虑到需要使用/usr/bin/python来导入Quartz,因此不适用于此代码.

This code works on Python2, but not 3 considering /usr/bin/python needs to be used to import Quartz.

推荐答案

安装quartz-
pip install pyobjc-framework-Quartz

很多简单的代码-

import Quartz
d = Quartz.CGSessionCopyCurrentDictionary()
print('CGSSessionScreenIsLocked' in d.keys())

这篇关于通过Python3在Mac上检测锁定屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆