How to disable right-click context menus in NSTextField (Cocoa)?

I am working on a Cocoa application with editable text fields. These text fields should take values, but do not need to check spelling or use any other parameters specified in the default context menu. I read that the easiest way to remove the right-click context menu of the mouse / select + click is to override the function:

rightMouseDown:(NSEvent *) 

I did this in the regular NSTextfield class. This fix blocks the user’s right-clicking when the text field is enabled and not selected, but as soon as the user double-clicks / enters the text field to edit the default right-click functionality.

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

Thanks!

+2
source share
1 answer

When a text field is edited, the actual first responder is the "field editor", NSTextView , provided by the window. The field editor always uses the control on behalf of which it acts as its delegate.

So, in order to influence its behavior in the context of the context menu, you need to use your own subclass of NSTextField . (I think you should already override -rightMouseDown: ) Then we implement the text view delegate method -textView:menu:forEvent:atIndex: and return nil (i.e. No menu).

+5
source

Source: https://habr.com/ru/post/1493668/


All Articles