以编程方式启用对辅助设备的访问

发布于 2024-09-03 08:46:27 字数 461 浏览 11 评论 0原文

我想以编程方式在系统偏好设置中启用对辅助设备的访问。但问题是我的应用程序没有以 root 用户身份运行,我不希望我的应用程序以 root 用户身份运行,也不应该要求其间进行任何身份验证。

我想点击全球所有键盘事件。我正在使用 CGEventTapCreate() 进行相同的操作。在 CGEventTapCreate() API 的文档中提到,如果满足以下条件之一,事件点击会接收按键向上和按键向下事件:

  1. 当前进程正在以根用户身份运行。
  2. 已启用辅助设备的访问。在 Mac OS X v10.4 和稍后,您可以使用系统偏好设置、通用访问面板、键盘视图启用此功能。

我通过检查“系统偏好设置”中的“启用辅助设备的访问”来手动尝试,它给了我预期的输出。

那么有没有什么方法可以通过程序执行相同的操作而不要求身份验证并且应用程序不以 root 用户身份运行?

谢谢,

黛拉吉。

I want to enable Access for assistive devices in System Preferences programmatically. But Problem is that my application is not running as root user and i do not want my application to be as root user and also should not ask for any authentication in between.

I want to tap all keyboard events globally. I am using CGEventTapCreate() for the same.In the documentation of CGEventTapCreate() API it is mentioned that, Event taps receive key up and key down events if one of the following conditions is true:

  1. The current process is running as the root user.
  2. Access for assistive devices is enabled. In Mac OS X v10.4 & later, you can enable this feature using System Preferences, Universal Access panel, Keyboard view.

I tried manually by checking the Enable Access for assistive devices from System Preference and it gives me expected output.

So is there any way to do the same via program without asking for authentication and also application is not running as root user?

Thanks,

Dheeraj.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雨后彩虹 2024-09-10 08:46:27

您可以运行 Applescript(或将 Applescript 转换为 ScriptingBridge 或 AppleEvents 上的任何 Objective-C 层)

以下是我在一个特定项目中使用的 Applescript,它执行与您需要的类似操作:

on isUIScriptingOn()
    tell application "System Events" to set isUIScriptingEnabled to UI elements enabled
    return isUIScriptingEnabled
end isUIScriptingOn

on turnUIScriptingOn(switch)
    tell application "System Events"
        activate
        set UI elements enabled to switch
    end tell
end turnUIScriptingOn

on run
    if not isUIScriptingOn() then
        display dialog "Enable Access for assistive devices (found in the Universal Access System Preference) must be on for this software to correctly work. This program will enable this setting for you"
        turnUIScriptingOn(true)
        display dialog "Access for assistive devices in now on"
    end if
end run

You can run an Applescript (or translate the Applescript into ScriptingBridge or whatever your Objective-C layer over AppleEvents is)

Here is an Applescript that I use in one particular project that does something similar to what you need:

on isUIScriptingOn()
    tell application "System Events" to set isUIScriptingEnabled to UI elements enabled
    return isUIScriptingEnabled
end isUIScriptingOn

on turnUIScriptingOn(switch)
    tell application "System Events"
        activate
        set UI elements enabled to switch
    end tell
end turnUIScriptingOn

on run
    if not isUIScriptingOn() then
        display dialog "Enable Access for assistive devices (found in the Universal Access System Preference) must be on for this software to correctly work. This program will enable this setting for you"
        turnUIScriptingOn(true)
        display dialog "Access for assistive devices in now on"
    end if
end run
始于初秋 2024-09-10 08:46:27

好的,解决方案/一些背景信息 可以在这个地址找到..

因此,Apple 在 Tiger 中提出了另一个解决方案来解决这些问题:神奇的函数 AXMakeProcessTrusted。这将使 API 仅适用于您的应用程序,并且需要从以 root 身份运行的进程中调用,因此它是安全的。它也是全自动的,因此除了询问用户他/她的密码之外,用户无需执行任何操作。
问题是好像没人用。我见过的每个第三方应用程序,甚至 Automator 都只是要求用户手动选中系统偏好设置中的复选框。实现起来需要更多工作,并且有一个巨大的未记录错误(应用程序必须重新启动才能真正受信任更新:报告为#5711990),但我真的认为人们应该使用它。所以,我想我应该发布代码,以便在您的应用程序中轻松实现它。它包括一个帮助代理,您应该可以将其放入您的项目中。

Ok y'all the solution / some background info can be found at this address..

So, Apple came up with another solution in Tiger that solves these problems: the magic function AXMakeProcessTrusted. That will enable the API just for your application and needs to be called from a process running as root, so it is secure. It is also all automatic, so aside from asking the user for his/her password, there is nothing the user needs to do.
The problem is that it seems no one uses it. Every third party application I have seen and even Automator just asks the user to manually check the box in System Preferences. It is more work to implement and has one huge undocumented bug (the application must be relaunched before it is actually trusted Update: reported as #5711990), but I really think people should be using it. So, I thought I would release the code to make it easy to implement it in your app. It includes a helper agent that you should be able to just drop into your project.

烙印 2024-09-10 08:46:27

如果没有明确请求许可,则在不至少告知用户的情况下更改用户的系​​统设置通常被认为是粗鲁的。大多数需要此设置的应用程序只需检查它是否启用,如果没有,则告诉用户将其打开。

It's usually considered rude to change a user's system setting without at least telling them, if not explicitly asking for permission. Most apps that need this setting just check to see if it's enable and if not, tell the user to turn it on.

自我难过 2024-09-10 08:46:27

有一种方法可以在每个进程的基础上启用它。不幸的是,我不知道它是什么,我有可以执行此操作的应用程序,我想我记得在可可开发邮件列表上看到过一些关于它的内容

您确定可以在每个进程的基础上启用它,而不需要管理员权限?如果是,是捕获所有事件,还是仅捕获属于该进程的事件?

There is a way to enable it on a per-process basis. Unfortunately i dont know what it is, i have apps that do this tho and i think i recall seeing something about it on the cocoa dev mailing list

Are you sure that it's possible to enable it on a per-process basis, without needing to have admin rights? And if yes, will all events be captures, or only the ones belonging to the process?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文