I have a small problem right now. I want to execute a method when the Enter key is pressed in a NSTextField. The user should be able to enter his data and a calculation meth
You can do this by setting the text field's action. In IB, wire the text field's selector to your controller or whatever object presents the IBAction you want to use.
To set it in code, send the NSTextField a setTarget:
message and a setAction:
message. For example, if you're setting this on your controller object in code, and your textField outlet is called myTextField:
- (void)someAction:(id)sender
{
// do something interesting when the user hits <enter> in the text field
}
// ...
[myTextField setTarget:self];
[myTextField setAction:@selector(someAction:)];
In your delegate (NSTextFieldDelegate), add the following:
-(void)controlTextDidEndEditing:(NSNotification *)notification
{
// See if it was due to a return
if ( [[[notification userInfo] objectForKey:@"NSTextMovement"] intValue] == NSReturnTextMovement )
{
NSLog(@"Return was pressed!");
}
}
The Swift 3 version of @M.ShuaibImran's solution:
First subclass your ViewController to: NSTextFieldDelegate
class MainViewController: NSViewController, NSTextFieldDelegate {
...
}
Assign the textField delegate to the ViewController in your viewDidLoad():
self.textField.delegate = self
Include the NSTextFieldDelegate method that handles keyboard responders:
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
}
Best way to do that is to bind the NSTextField
value with NSString
property.
For Example,define a method:
(void)setTextFieldString:(NSString *)addressString {}
add bindings:
[textField bind:@"value" toObject:self withKeyPath:@"self.textFieldString" options:nil];
Enter any text and hit the return key, setTextFieldString
will be called.
In Interface Builder - click on your NSTextField, go to the connections editor, drag from selector to your controller object - you're actions should come up!
It's very easy and you can do it directly from UI editor
Note: The event will be raised as soon as you press Tab
or Enter
key. In case you want the action should only be raised when user presses the Enter
key then you have to do a setting. Go to the Attribute inspector
and change the Action property to Send on Enter only
as shown in screen shot below