How to do something when Enter-key is pressed on NSTextField

macos, nstextfield, swift, xcode6

Solution

@IBAction func textFieldAction(sender: NSTextField) {
    print(sender.stringValue)
}

Problem

I'm writing an app for the Mac using Swift. I write a string inside a NSTextField object and I would like to save it in a .txt file. I would like that to happen as soon as the user presses Enter-key. My method `.writeToFile()` is ready, I cannot figure out how to make it run as soon as that key is pressed. I could not find a proper answer for Swift language. Thank you.

Original source

Related problems