Reputation: 13557
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 method should be executed as soon as he hits the enter key.
Upvotes: 76
Views: 47715
Reputation: 818
The Swift 3 / 4 / 5 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
}
Upvotes: 35
Reputation: 1307
You have to do only this
For some keys (Enter, Delete, Backspace, etc)
self.textfield.delegate = self;
and then implement this method
- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
{
NSLog(@"Selector method is (%@)", NSStringFromSelector( commandSelector ) );
if (commandSelector == @selector(insertNewline:)) {
//Do something against ENTER key
} else if (commandSelector == @selector(deleteForward:)) {
//Do something against DELETE key
} else if (commandSelector == @selector(deleteBackward:)) {
//Do something against BACKSPACE key
} else if (commandSelector == @selector(insertTab:)) {
//Do something against TAB key
} else if (commandSelector == @selector(cancelOperation:)) {
//Do something against Escape key
}
// return YES if the action was handled; otherwise NO
}
Upvotes: 55
Reputation: 11730
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
Upvotes: 18
Reputation: 78343
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:)];
Upvotes: 84
Reputation: 229
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.
Upvotes: 1
Reputation: 1058
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!");
}
}
Upvotes: 26
Reputation: 6882
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!
Upvotes: 1
Reputation: 1110
NSTextFieldDelegate
's – control:textView:doCommandBySelector:
is your friend.
Look for the insertNewline:
command.
Upvotes: 4