Trying to find a way to detect if the screen is locked on a Mac system, using Python3
I've tried the following, and it gives a KeyError:
import Quartz
gui_dict = Quartz.CGSessionCopyCurrentDictionary()
print(gui_dict['CGSSessionScreenIsLocked'])
edit:
import Quartz
all_windows = Quartz.CGWindowListCopyWindowInfo(Quartz.kCGWindowListOptionOnScreenOnly, Quartz.kCGNullWindowID)
for x in all_windows:
if x["kCGWindowOwnerName"] == "loginwindow"
print("Locked!")
This code works on Python2, but not 3 considering /usr/bin/python needs to be used to import Quartz.
Install quartz
-
pip install pyobjc-framework-Quartz
Much simple code -
import Quartz
d = Quartz.CGSessionCopyCurrentDictionary()
print('CGSSessionScreenIsLocked' in d.keys())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.