1

I have a need to capture the mouseDown event in an NSTextField, which is easily achievable with NSTextField.isSelectable = false. However, turning off the selectable flag disables text selection, and so I assume that I need to roll my own, so to speak, and set the selection as the mouse is moved over the text. I've scoured the online resources, must of which point me in the direction of using the NSTextField field editor, and I've not had any success on that path. For example, this code snippet doesn't accomplish anything in my testing:

    override func mouseDragged(with event: NSEvent)
    {
        print (ME + ".\(#function)")
        let fieldEditor = self.currentEditor()
        fieldEditor?.selectedRange = NSMakeRange(0, 2)
    }

Some online resources led me in the direction of the NSTextViewDelegate, but those methods aren't being called.

So somewhere in all this I'm obviously terribly confused. So my question is this - how do I detect and visually indicate text selection in an NSTextField that is configured to allow mouse events? Any pointers to a workable solution are greatly appreciated.

Hayseed
  • 21
  • 4
  • @Willeke - That worked. I do appreciate the pointer. There appears some awkwardness with the double click and select, but that's another problem for another. I appreciate your help. – Hayseed Mar 25 '21 at 16:32

0 Answers0