如何在 Cocoa 应用程序包内的 dict 文件中指定键绑定?
(这不是关于 KVC/KVO 和键值绑定的问题。)
我正在编写一个带有“工具”面板的应用程序。每个工具都可以使用一个字母键来选择(如“M”-“移动工具”)。即使没有主窗口并且未选择工具面板(不是“键”),也可以选择工具。
文档说原始按键事件不会传播到应用程序委托和文档控制器(因此我不能依赖 onKeyDown,因为它应该发生在 WindowController 中)。但“操作事件”(绑定到选择器的键)会发送到应用程序、应用程序委托和文档控制器。
我知道设置此类全局绑定的两种方法:通过 Interface Builder 中的 NSMenuItem 键等效项或使用 ~/Library/KeyBindings/DefaultKeyBinding.dict 文件(它指定所有应用程序的全局绑定)。
菜单项方法不好,因为我不想用可以从工具面板触发的命令污染我的菜单。
我想以编程方式设置此类绑定,或者像 Xcode 和 TextMate 一样在捆绑的 KeyBindings.dict 中指定它们。
但是,我找不到如何命名此类 dict 文件以及如何将其连接到应用程序的文档或示例。 Xcode 使用 PBKeyBinding.dict 文件名,TextMate 使用 KeyBindings.dict。 Info.plist 没有选项告诉应用程序读取 dict 文件。
有谁知道如何做到这一点?
谢谢。
(This is not a question about KVC/KVO and key-value bindings.)
I'm writing an application with a "tools" panel. Every tool can be selected using one-letter key (like "M" - "Move tool"). Tool can be selected even when there is no main window and the tools panel is not selected (not being "key").
Docs say that raw key events are not propagated to the app delegate and document controller (so I cannot rely on onKeyDown since it should happen in a WindowController). But "action events" (keys bound to a selector) are sent to the app, app delegate and document controller.
I know two ways to set such global bindings: through NSMenuItem key equivalents in Interface Builder or using ~/Library/KeyBindings/DefaultKeyBinding.dict file (which specifies global bindings for all apps).
Menu items approach is not good because I don't want to pollute my menu with commands which can be triggered from the tools panel.
I would like to set such bindings programmatically or specify them in a bundled KeyBindings.dict like Xcode and TextMate do.
However, I couldn't find docs or examples how to name such dict file and how to connect it to the app. Xcode uses PBKeyBinding.dict filename, TextMate uses KeyBindings.dict. There's no option for Info.plist to tell app to read the dict file.
Does anybody know how this could be done?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
文档表示不支持此操作。< /a> 所以,即使你让它工作,它也可能会在 Mac OS X 的未来版本中崩溃。
我建议将所有工具放入“工具”菜单中。冗余本质上并不是坏事,菜单可以让视力不佳的人更轻松地导航,并且可以为那些还不熟悉工具图标的人提供快速参考。
如果“工具”菜单确实没有意义,可以设置“工具”菜单项(其子菜单是“工具”菜单)的“隐藏”属性。这样,“工具”菜单项仍位于主菜单(菜单栏)中,因此仍应将其键绑定与操作相关联,但它对用户不可见。
The documentation says this isn't supported. So, even if you get it working, it could break in a future version of Mac OS X.
I suggest putting all of the tools into a Tools menu. Redundancy is not inherently bad, and a menu could be more easily navigable by people with poor vision, and would serve as a quick reference for those not yet familiar with your tool icons.
If a Tools menu really is pointless, you can set the Hidden property of the Tools menu item (whose submenu is the Tools menu). This way, the Tools menu item is still in the main menu (menu bar), and so should still associate its key bindings with actions, but it is not visible to users.
事实上,这个库满足了我的需要:
http://github.com/carpeaqua/SHotKeysLib
使用RegisterEventHotKey函数(需要与Carbon框架链接)。
In fact, this library does what I need:
http://github.com/carpeaqua/SGHotKeysLib
It uses RegisterEventHotKey function (requires linking with Carbon framework).