辅助功能 API - 可可
有没有什么方法(我认为使用辅助功能 API)能够在我的应用程序中按下按钮/按键,然后将鼠标悬停在菜单栏项上并再次按下,然后返回菜单项的名称、它所在的菜单以及应用程序?
我想在 Cocoa/Objective-C (Mac) 中执行此操作。
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?
I want to do this in Cocoa/Objective-C (Mac).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是
kAXMenuItemSelectedNotification
通知的用途。玩这个东西的一个简单方法是使用 UI 浏览器:这可以让您看到通知何时被触发。您的代码可能如下所示:
请注意,您必须单独观察每个正在运行的应用程序(以及启动/退出时的其他应用程序),或者注册
NSWorkspaceDidActivateApplicationNotification
并取消注册/注册最前面的应用程序此时(后者在 10.6 下可能是最简单的,因为您会收到 NSRunningApplication 作为通知的一部分)。我认为实际选择该项目对用户来说会更容易,但如果您确实想按另一个键来完成操作,您可以观察
kAXSelectedChildrenChanged
,当选择从一个菜单项到另一个菜单项。That's what the
kAXMenuItemSelectedNotification
notification is for. An easy way to play with this stuff is using UI Browser:That lets you see when notifications are triggered. Your code could look something like this:
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 aNSRunningApplication
as part of the notification).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.