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 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
}
NSTextFieldDelegate
's – control:textView:doCommandBySelector:
is your friend.
Look for the insertNewline:
command.