简体   繁体   English

如何通过右键单击NSTextField(Cocoa)禁用上下文菜单?

[英]How to disable context menus with right mouse click in an NSTextField (Cocoa)?

I'm working on a Cocoa application that has editable text fields. 我正在使用具有可编辑文本字段的Cocoa应用程序。 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: 我已经读到,删除右键单击/ opt +单击上下文菜单的最简单方法是覆盖此功能:

rightMouseDown:(NSEvent *) 

I've done this in a custom NSTextfield class. 我已经在自定义NSTextfield类中完成了此操作。 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. 此修复程序会阻止用户在启用和未选中文本框时进行右键单击,但是一旦用户双击/输入用于编辑默认右键单击功能的文本字段,该用户便会返回。

Is this because the firstResponder switches to a class in the inheritance chain upon trying to edit the field? 这是因为在尝试编辑字段时firstResponder切换到继承链中的类吗? Is this approach the right way to disable all context menu functionality for this NSTextField? 这种方法是禁用此NSTextField的所有上下文菜单功能的正确方法吗?

Thanks! 谢谢!

When a text field is editing, the actual first responder is the "field editor", an NSTextView supplied by the window. 编辑文本字段时,实际的第一响应者是“字段编辑器”,即NSTextView提供的NSTextView The field editor always uses the control on whose behalf it is acting as its delegate. 字段编辑器始终使用其代表其控件的控件。

So, in order to influence its behavior with respect to the contextual menu, you need to use a custom subclass of NSTextField . 因此,为了影响其相对于上下文菜单的行为,您需要使用NSTextField的自定义子类。 (I guess you already are to override -rightMouseDown: .) Then, implement the text view delegate method -textView:menu:forEvent:atIndex: and return nil (ie no menu). (我想您已经覆盖-rightMouseDown: )然后,实现文本视图委托方法-textView:menu:forEvent:atIndex:并返回nil (即无菜单)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM