如何在 NSTextView 中禁用这些键盘快捷键?

发布于 2024-12-16 18:53:57 字数 215 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眉目亦如画i 2024-12-23 18:53:58

首先,请确保您了解 关键事件路径用户界面验证

我认为禁用您提到的操作的最佳方法是通过子类化 NSTextView 并通过声明您的文本视图符合 NSUserInterfaceValidations 并编写验证方法来禁用其关联的菜单项:

- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)anItem {

    SEL action = [anItem action];
    if (@selector(selectAll:) == action ||                    // command-a
        @selector(centerSelectionInVisibleArea:) == action || // command-j
        @selector(print:) == action ||                        // command-p
        @selector(underline:) == action) {                    // command-u

        return NO;
    }
    else return [super validateUserInterfaceItem:anItem];
}

但这并不能阻止用户通过 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:

- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)anItem {

    SEL action = [anItem action];
    if (@selector(selectAll:) == action ||                    // command-a
        @selector(centerSelectionInVisibleArea:) == action || // command-j
        @selector(print:) == action ||                        // command-p
        @selector(underline:) == action) {                    // command-u

        return NO;
    }
    else return [super validateUserInterfaceItem:anItem];
}

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:

街角迷惘 2024-12-23 18:53:58

覆盖这些组合键的 NSEvents 以不执行任何操作,而不是执行它们通常执行的操作。

Override the NSEvents for these key combinations to do nothing instead of what they will normally be doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文