avatarKhoa Pham

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

989

Abstract

r: <span class="hljs-literal">nil</span>) <span class="hljs-keyword">as?</span> <span class="hljs-type">NSTextView</span> textView<span class="hljs-operator">?</span>.insertionPointColor <span class="hljs-operator">=</span> <span class="hljs-type">R</span>.nsColor.action onFocusChange(<span class="hljs-literal">true</span>) <span class="hljs-keyword">return</span> <span class="hljs-keyword">super</span>.becomeFirstResponder() } }</pre></div><div id="78e7"><pre>textField.delegate <span class="hljs-comment">// NSTextFieldDelegate</span> <span class="hljs-keyword">func</span> <span class="hljs-title function_">controlTextDidEndEditing</span>(<span class="hljs-keyword">_</span> <span class="hljs-params">obj</span>: <span class="hljs-type">Notification</span>) { onFocusChange(<span class="hljs-literal">false</span>) }</pre></div><h1 id="5761">NSTextField and NSText</h1><p id="27f5"><a href="https://stackoverflow.com/questions/25692122/how-to-detect-

Options

when-nstextfield-has-the-focus-or-is-its-content-selected-cocoa">https://stackoverflow.com/questions/25692122/how-to-detect-when-nstextfield-has-the-focus-or-is-its-content-selected-cocoa</a></p><blockquote id="8312"><p><i>When you clicked on search field, search field become first responder once, but NSText will be prepared sometime somewhere later, and the focus will be moved to the NSText.</i></p></blockquote><blockquote id="b45c"><p><i>I found out that when NSText is prepared, it is set to self.currentEditor() . The problem is that when becomeFirstResponder()’s call, self.currentEditor() hasn’t set yet. So becomeFirstResponder() is not the method to detect it’s focus.</i></p></blockquote><blockquote id="767a"><p><i>On the other hand, when focus is moved to NSText, text field’s resignFirstResponder() is called, and you know what? self.currentEditor() has set. So, this is the moment to tell it’s delegate that that text field got focused</i></p></blockquote></article></body>

How to observe focus event of NSTextField in macOS

becomeFirstResponder

class FocusAwareTextField: NSTextField {
    var onFocusChange: (Bool) -> Void = { _ in }
    override func becomeFirstResponder() -> Bool {
        let textView = window?.fieldEditor(true, for: nil) as? NSTextView
        textView?.insertionPointColor = R.nsColor.action
        onFocusChange(true)
        return super.becomeFirstResponder()
    }
}
textField.delegate // NSTextFieldDelegate
func controlTextDidEndEditing(_ obj: Notification) {
    onFocusChange(false)
}

NSTextField and NSText

https://stackoverflow.com/questions/25692122/how-to-detect-when-nstextfield-has-the-focus-or-is-its-content-selected-cocoa

When you clicked on search field, search field become first responder once, but NSText will be prepared sometime somewhere later, and the focus will be moved to the NSText.

I found out that when NSText is prepared, it is set to self.currentEditor() . The problem is that when becomeFirstResponder()’s call, self.currentEditor() hasn’t set yet. So becomeFirstResponder() is not the method to detect it’s focus.

On the other hand, when focus is moved to NSText, text field’s resignFirstResponder() is called, and you know what? self.currentEditor() has set. So, this is the moment to tell it’s delegate that that text field got focused

Macos
Focus
Nstextfield
Swif
Recommended from ReadMedium