Lets go from the start
Created New Xcode project,opened interface builder dragged one NSButton and one NSTextField over the NSWindow.
Created…..,OK this is my following Code
#import <Cocoa/Cocoa.h>
@interface SampleTextFieldButtonAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
IBOutlet NSTextField *textField;
}
@property (assign) IBOutlet NSWindow *window;
- (IBAction)button:(NSButton *)myBtn;
@end
#import "SampleTextFieldButtonAppDelegate.h"
@implementation SampleTextFieldButtonAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (void)controlTextDidEndEditing:(NSNotification *)obj
{
NSString *textFieldString = [textField stringValue];
NSLog(@"controlTextDidEndEditing %@",textFieldString);
}
- (IBAction)button:(NSButton *)myBtn
{
NSLog(@"button");
}
@end
Went to interface Builder,
connected textField outlet to NSTextField control,and connected delegate outlet of my textField to my SampleTextFieldButtonAppDelegate object
connected button action to NSButton control.
Assigned button key Equiv. to Return/Enter Key
Ran the XCode,initially textfield is focussed,pressed tab key from the keyboard,focus has changed to button,pressed tab key again focus went to textField.This time I typed some text in my text field and pressed Return Key.
See the following Log message I got,
2012-03-31 15:37:37.723 SampleTextFieldButton[1845:a0f] controlTextDidEndEditing
2012-03-31 15:37:41.601 SampleTextFieldButton[1845:a0f] controlTextDidEndEditing dasdj
2012-03-31 15:37:41.629 SampleTextFieldButton[1845:a0f] button
Why the button action is getting called while I am interacting with my NSTextField?Please give me some solution for this.
Assigned button key Equiv. to Return/Enter Key
Ran the XCode,initially textfield is focussed,pressed tab key from the keyboard,focus has changed to button,pressed tab key again focus went to textField.This time I typed some text in my text field and pressed Return Key.
Because you’re pressing the enter key of your keyboard. That’s why keypress equiv are often combined with modifiers (cmd, alt, shift)
imagine if instead of printing shortcut “cmd+p”, the shortcut was just “p”. My computer would have launched printing 5 times writing these two lines 🙂
You can make direct key bindings, like when you build a game for example, where “z” is used to go further and “q” turn left, etc. but be sure that’s not used during text writing for example. Just disable the key binding on your button on the textDidBeginEditing event and put back the binding on textDidEndEditing.