辅助功能API - Cocoa [英] Accessibility API - Cocoa
问题描述
有任何方式(我认为使用辅助功能API)能够在我的应用程序中按下按钮/按键,然后将鼠标悬停在菜单项上,再按一次,然后返回菜单项的名称,菜单in and the app?
Is there any way (I think using the Accessibility API) to be able to press a button/keystroke in my app and then hover over a menubar item and press again and then return the name of the menu item, the menu it's in and the app?
我想在Cocoa / Objective-C(Mac)中执行此操作。
I want to do this in Cocoa/Objective-C (Mac).
推荐答案
这是 kAXMenuItemSelectedNotification
通知的用途。使用此功能的一个简单方法是使用 UI浏览器:
That's what the kAXMenuItemSelectedNotification
notification is for. An easy way to play with this stuff is using UI Browser:
- 在目标弹出式菜单中选择一个应用。
- 从抽屉弹出式菜单中选择通知。
- 点击选择菜单项和注册(或只需双击表格)。
- 从查看菜单中选择通知日志 li>
- 切换到您选择的应用程序并选择菜单项。
- Pick an application in the "Target" popup menu.
- Pick "Notifications" from the "Drawer" popup menu.
- Click "menu item selected" and "Register" (or just double-click in the table).
- Choose "Notification Log" from the View menu.
- Switch to the application you chose and pick a menu item.
被触发。您的代码可能如下所示:
That lets you see when notifications are triggered. Your code could look something like this:
AXError err;
AXObserverRef observer;
pid_t currentAppPID = [NSRunningApplication currentApplication].processIdentifier;
if ( (err = AXObserverCreate(currentAppPID, notificationCallback, &observer) != kAXErrorSuccess); // XXX failed
CFRunLoopAddSource(CFRunLoopGetCurrent(), AXObserverGetRunLoopSource(observer), kCFRunLoopDefaultMode);
AXUIElementRef element = AXUIElementCreateApplication(currentAppPID);
if (element == NULL); // XXX failed
if ( (err = AXObserverAddNotification(observer, element, kAXMenuItemSelectedNotification, NULL)) != kAXErrorSuccess); // XXX failed
请注意,在注册 NSWorkspaceDidActivateApplicationNotification
并注销最前端的应用程序(后者可能是最容易在10.6以下,因为它是最简单的,因为它是启动/退出)你会得到 NSRunningApplication
作为通知的一部分。)
Note that you'll have to observe every running app individually (and additional apps as they launch/quit), or register for NSWorkspaceDidActivateApplicationNotification
and deregister/register with the frontmost app at that point (the latter is probably easiest under 10.6 because you get a NSRunningApplication
as part of the notification).
我认为实际选择项目会更容易一个用户,但如果你真的想要按另一个键来完成操作,你可以观察 kAXSelectedChildrenChanged
,当选择从一个菜单项移动到另一个。
I would think actually selecting the item would be easier for a user, but if you really do want to press another key to complete the action, you can observe kAXSelectedChildrenChanged
, which will be triggered when the selection moves from one menu item to another.
这篇关于辅助功能API - Cocoa的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!