编辑 NSTextField 时添加热键
是否可以仅在编辑某个 NSTextField 时添加热键?例如,当您在此文本字段中时,我希望您按 Command + 1 插入一个字符串,按 Command + 2 插入另一个文本字符串等,但仅当您编辑该 NSText 字段时。
我尝试将 keyDown 方法添加到我的子类 NSTextField 中,但这似乎从未被触发。如果我将其更改为 keyUp,它将被触发,但如果按住命令键则不会。
似乎当我搜索热键时,我找到的唯一信息是全局热键(即使应用程序不可见时也会激活的热键),这不是我正在寻找的信息。
Is it possible to add hotkeys only when you're editing a certain NSTextField? For example, when you're in this text field I would like it if you pressed Command + 1 it inserted one string, and Command + 2 to insert another text string, etc, but only when you're editing that NSText field.
I tried adding a keyDown method to my subclassed NSTextField, but that didn't seem to ever get fired. If I changed it to keyUp it gets fired, but not if the command key is held down.
It seems when I'm searching for hotkeys the only information I find is on global hotkeys (ones that activate even when the app isn't visible) which isn't what I'm looking for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您有一个带有该键盘快捷键的菜单项,则可以实现
validateUserInterfaceItem:
以在您的字段位于响应程序链中时启用它。我不确定这是否适用于隐藏的菜单项。If you had a menu item with that keyboard shortcut, you could implement
validateUserInterfaceItem:
to enable it when your field is in the responder chain. What I'm not sure about is whether that would work for a hidden menu item.NSTextField 不处理 keyDown: 本身。相反,他们似乎依赖于称为“字段编辑器”的中介(从此处的类似问题中找到此信息 http://www.cocoabuilder.com/archive/cocoa/51299-custom-nstextfield-keydown.html)。
苹果有一些关于这个主题的更多信息 - http ://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TextEditing/Tasks/FieldEditor.html
NSTextField's to not deal with keyDown: themselves. They instead seem to rely on intermediaries called Field Editors (found this info from a similar question here http://www.cocoabuilder.com/archive/cocoa/51299-custom-nstextfield-keydown.html).
Apple has some more info on the subject - http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TextEditing/Tasks/FieldEditor.html
当我的文本字段是第一响应者时,我最终对窗口进行了子类化并覆盖了 sendEvent 。
I ended up subclassing my window and overriding sendEvent when my text field is the first responder.