如何通过右键单击NSTextField(Cocoa)禁用上下文菜单? [英] How to disable context menus with right mouse click in an NSTextField (Cocoa)?

查看:192
本文介绍了如何通过右键单击NSTextField(Cocoa)禁用上下文菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用具有可编辑文本字段的Cocoa应用程序.这些文本字段需要接受值,但不需要进行拼写检查或使用默认上下文菜单中提供的任何其他选项.我已经读到,删除右键单击/opt +单击上下文菜单的最简单方法是覆盖此功能:

I'm working on a Cocoa application that has editable text fields. These text fields need to accept values but don't need to be spell checked or use any of the other options given in the default context menu. I've read that the easiest way to remove the right click/ opt + click context menu is to override the function:

rightMouseDown:(NSEvent *) 

我已经在自定义NSTextfield类中完成了此操作.此修复程序会阻止用户在启用和未选中文本框时进行右键单击,但是一旦用户双击/输入用于编辑默认右键单击功能的文本字段,该用户便会返回.

I've done this in a custom NSTextfield class. This fix blocks the user from right clicking when the text box is enabled and unselected, but as soon as the user double clicks/enters the text field for editing the default right click functionality returns.

这是因为在尝试编辑字段时firstResponder会切换到继承链中的类吗?这种方法是禁用此NSTextField的所有上下文菜单功能的正确方法吗?

Is this because the firstResponder switches to a class in the inheritance chain upon trying to edit the field? Is this approach the right way to disable all context menu functionality for this NSTextField?

谢谢!

推荐答案

在编辑文本字段时,实际的第一响应者是窗口提供的NSTextView字段编辑器".字段编辑器始终使用其代表其控件的控件.

When a text field is editing, the actual first responder is the "field editor", an NSTextView supplied by the window. The field editor always uses the control on whose behalf it is acting as its delegate.

因此,为了影响其相对于上下文菜单的行为,您需要使用NSTextField的自定义子类. (我想您已经覆盖了-rightMouseDown:.)然后,实现文本视图委托方法-textView:menu:forEvent:atIndex:并返回nil(即无菜单).

So, in order to influence its behavior with respect to the contextual menu, you need to use a custom subclass of NSTextField. (I guess you already are to override -rightMouseDown:.) Then, implement the text view delegate method -textView:menu:forEvent:atIndex: and return nil (i.e. no menu).

这篇关于如何通过右键单击NSTextField(Cocoa)禁用上下文菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆