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

2020-03-30 07:43发布

问题:

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.

回答1:

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