通过macOS中的API锁定屏幕 [英] Lock screen by API in macOS

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

问题描述

是否有一个可以将屏幕锁定为菜单栏条目的API,您可以从Keychain首选项中添加它?

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

Keychain功能用于锁定屏幕,但不会使系统进入睡眠状态.

This Keychain function is (was) locking the screen but not bringing the system to sleep.

推荐答案

它尚未正式记录,并使用私有API,但以下内容可在MacOS 10.10(甚至可能在较早的系统上)上使用:

It's not officially documented and uses private API, but the following works on MacOS 10.10 (and maybe also on earlier systems):

// lockscreen.c
extern int SACLockScreenImmediate ( void );

int main ( ) {
    return SACLockScreenImmediate();
}

构建方式:

clang -F /System/Library/PrivateFrameworks -framework login -o lockscreen lockscreen.c 

现在调用./lockscreen将立即锁定屏幕,无论用户在其安全性首选项中配置了什么(是否锁定屏幕保护程序/系统睡眠),并且都不会注销用户.这是系统内部用于锁定屏幕的功能.

Now calling ./lockscreen will lock the screen immediately, regardless what the user has configured in their security preferences (whether to lock on screensaver/system sleep) and without logging the user out. This is the function the system uses internally for locking the screen.

我强烈建议不要使用它,它可能会破坏您的应用程序,甚至不确定我是否正确调用了它(也许它需要参数,也许它具有返回值),因此它甚至有可能破坏您的整个系统(暂时) -重新启动将解决所有问题),谁知道.我只是想将其张贴在文档中.

I strongly discourage using it, it may break your app and I'm not even sure I am calling it correctly (maybe it needs arguments, maybe it has a return value), so it may even break your whole system (temporarily - reboot will fix everything), who knows. I just wanted to post that somewhere for documentation.

如果黑客技能比我更好的人可以对此电话进行更多分析,那么这将是很好并且有用的.

If someone with better hacker skills than me can analyze this call some more, this would be nice and useful.

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

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