Q:

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

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.

A:

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

