Get KeyDown event for NSTextField

In the latest version of xcode, I am trying to get the keyDown event for an NSTextField. However, despite the following several online tutorials (delegates, controllers ...), I still cannot get them.

Any easy hint for me?

Thanks!

+7
source share
3 answers

I was tired of all the answers to do it differently, so I put my nose down and figured out a way to make this work. This does not use the keydown event directly, but it uses keydown in the block. And the behavior is exactly what I wanted.

Text box subclass

.h

@interface LQRestrictedInputTextField : NSTextField 

.m

In setting up the first responder, a local event

 static id eventMonitor = nil; - (BOOL)becomeFirstResponder { BOOL okToChange = [super becomeFirstResponder]; if (okToChange) { [self setKeyboardFocusRingNeedsDisplayInRect: [self bounds]]; if (!eventMonitor) { eventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *event) { NSString *characters = [event characters]; unichar character = [characters characterAtIndex:0]; NSString *characterString=[NSString stringWithFormat:@"%c",character]; NSArray *validNonAlphaNumericArray = @[@" ",@"(",@")",@"[",@"]",@":",@";",@"\'",@"\"",@".",@"<",@">",@",",@"{",@"}",@"|",@"=",@"+",@"-",@"_",@"?",@"#", @(NSDownArrowFunctionKey),@(NSUpArrowFunctionKey),@(NSLeftArrowFunctionKey),@(NSRightArrowFunctionKey)]; if([[NSCharacterSet alphanumericCharacterSet] characterIsMember:character] || character == NSCarriageReturnCharacter || character == NSTabCharacter || character == NSDeleteCharacter || [validNonAlphaNumericArray containsObject:characterString ] ) { //[NSCharacterSet alphanumericCharacterSet] } else { NSBeep(); event=nil; } return event; } ]; } } NSLog(@"become first responder"); return okToChange; } 

delete the event after editing the text field.

Also, if you use ARC, I noticed that you might need to assign a textview string to stringValue. I have nslog'd stringValue and the value has been saved. Without nslog, I had to assign a string to the notification object in stringValue so that it would not be freed.

 -(void) textDidEndEditing:(NSNotification *)notification { [NSEvent removeMonitor:eventMonitor]; eventMonitor = nil; NSTextView *textView=[notification object]; self.stringValue=textView.string; } 
+10
source

You can subclass NStextField and use keyUp, which works for NSTextField.

in .h

 @interface MyTextField : NSTextField <NSTextFieldDelegate> 

in .m

 -(void)keyUp:(NSEvent *)theEvent { NSLog(@"Pressed key in NStextField!"); } 
+3
source

Add a UITextFieldDelegate to your .h like this

 @interface ViewController : UIViewController <UITextFieldDelegate> { 

Then you can use this to detect every keystroke in the text box

 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 

Return YES to allow the inserted character in the field, but you can add any code there.

-2
source

All Articles