如何在 NSTextView 中禁用这些键盘快捷键?
我有一个 NSTextView,我将其用作字母的全屏画布。
以下组合键执行了我不想执行的操作(例如隐藏窗口、锁定计算机)。
我怎样才能禁用它们以阻止它们被调用?
- Command-a
- Command-h
- Command-j
- Command-p
- Command-u
I have an NSTextView which I'm kind of using as a full-screen canvas for letters.
The following key combinations do things that I don't want to do (e.g. hide my window, lock up the computer).
How can I disable them to stop them being invoked?
- Command-a
- Command-h
- Command-j
- Command-p
- Command-u
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,请确保您了解 关键事件路径和用户界面验证。
我认为禁用您提到的操作的最佳方法是通过子类化 NSTextView 并通过声明您的文本视图符合 NSUserInterfaceValidations 并编写验证方法来禁用其关联的菜单项:
但这并不能阻止用户通过 Command+H 隐藏应用程序。要禁用该组合键,您可以在 MainMenu XIB 中删除其等效键,也可以继承 NSApplication 并覆盖
hide:
First, make sure you understand the path of key events and user interface validation.
I think the best way to disable the actions you mention is by subclassing NSTextView and disabling their associated menu items by declaring your text view as conforming to
NSUserInterfaceValidations
and writing the validation method:However that doesn't prevent the user from hiding the app via Command+H. To disable that key combo you can either remove its key equivalent in the MainMenu XIB, or you can subclass NSApplication and override
hide:
覆盖这些组合键的 NSEvents 以不执行任何操作,而不是执行它们通常执行的操作。
Override the NSEvents for these key combinations to do nothing instead of what they will normally be doing.