简体   繁体   English

API 在 Mac OS X 中通过快速用户切换检测活动 session

[英]API to detect active session in Mac OS X with fast user switch

On Mac OS X Snow Leopard with fast user switching enabled, is there an API to detect whether or not my application is running in the active user session?在启用快速用户切换的 Mac OS X Snow Leopard 上,是否有 API 来检测我的应用程序是否在活动用户 session 中运行? Ie the session currently attached to screen and keyboard.即当前连接到屏幕和键盘的 session。

Either Objective-C or C++ is fine. Objective-C 或 C++ 都可以。

I avoided using User Switch Notifications and instead have found two other possibilities:我避免使用用户切换通知,而是发现了另外两种可能性:

  1. Use CGMainDisplayID() from Core Graphics.使用 Core Graphics 中的CGMainDisplayID() Store the main display ID when your app first starts and keep polling it.在您的应用首次启动时存储主显示 ID 并继续轮询它。 It will change to a different display ID when switching to another user.切换到另一个用户时,它将更改为不同的显示 ID。 Problem with this is it may also change for other reasons eg changing which display is the primary display in a multi-screen setup.问题在于它也可能因其他原因而改变,例如更改哪个显示器是多屏幕设置中的主显示器。

  2. Use CGSessionCopyCurrentDictionary() also from Core Graphics, and retrieve the kCGSessionOnConsoleKey Boolean value from the dictionary.也从 Core Graphics 使用CGSessionCopyCurrentDictionary() ,并从字典中检索kCGSessionOnConsoleKey Boolean 值。 This indicates whether your user session is attached to console.这表明您的用户 session 是否已连接到控制台。

Both of these require polling but this is fine for my purposes.这两个都需要轮询,但这对我的目的来说很好。 User Switch Notifications would be a better choice if you need to be event-driven.如果您需要事件驱动,用户切换通知将是一个更好的选择。

From " Introduction to Multiple User Environments " i can only think of one thing that could fit your needs: User Switch Notifications.从“ 多用户环境简介”中,我只能想到一件事可以满足您的需求:用户切换通知。 So if your application starts it clearly must be in the active session.因此,如果您的应用程序启动,它显然必须在活动的 session 中。 Now you can use a user switch notification for setting the new state, ie that the application does not run anymore in the active session.现在您可以使用用户切换通知来设置新的 state,即应用程序不再在活动 session 中运行。

This worked for me (10.14 / Swift 4)这对我有用(10.14 / Swift 4)

        //Fast user switch out
    NSWorkspace.shared.notificationCenter.addObserver(
        self,
        selector: #selector(becameInactive),
        name: NSWorkspace.sessionDidResignActiveNotification,
        object: nil
    )

    //Fast user switch bak in
    NSWorkspace.shared.notificationCenter.addObserver(
        self,
        selector: #selector(becameActive),
        name: NSWorkspace.sessionDidBecomeActiveNotification,
        object: nil
    )

    // Switching workspace (spaces)
    NSWorkspace.shared.notificationCenter.addObserver(
        self,
        selector: #selector(workspaceSwitched),
        name: NSWorkspace.activeSpaceDidChangeNotification,
        object: nil
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM