blog icon indicating copy to clipboard operation
blog copied to clipboard

How to handle enter key in NSTextField

Open onmyway133 opened this issue 4 years ago • 1 comments

textField.delegate = self
NSTextFieldDelegate

func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
    if (commandSelector == #selector(NSResponder.insertNewline(_:))) {
        // Do something against ENTER key
        print("enter")
        return true
    } else if (commandSelector == #selector(NSResponder.deleteForward(_:))) {
        // Do something against DELETE key
        return true
    } else if (commandSelector == #selector(NSResponder.deleteBackward(_:))) {
        // Do something against BACKSPACE key
        return true
    } else if (commandSelector == #selector(NSResponder.insertTab(_:))) {
        // Do something against TAB key
        return true
    } else if (commandSelector == #selector(NSResponder.cancelOperation(_:))) {
        // Do something against ESCAPE key
        return true
    }

    // return true if the action was handled; otherwise false
    return false
}

onmyway133 avatar Apr 03 '20 03:04 onmyway133

Thank you

netskyinc avatar Nov 13 '21 18:11 netskyinc