11

In xcode last version, I am trying to get the keyDown event of an NSTextField. However, despite following multiple tutorials on the internet (delegates, controllers...), I still can't receive it.

Any easy hint for me ?

Thanks !

Laurent Crivello
  • 3,809
  • 6
  • 45
  • 89

3 Answers3

14

I got sick of all the non answers to do it some other way people so I put my nose down and figured out a way to make this work. This isn't using keydown event directly but it is using the keydown in the block. And the behavior is exactly what I wanted.

Subclass the text field

.h

@interface LQRestrictedInputTextField : NSTextField 

.m

In the become first responder setup 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;
}

remove the event once the textfield editing ends

Also if you're using ARC I noticed you might need to assign the textview string to the stringValue. I nslog'd the stringValue and the value was retained. Without the nslog I had to assign the notification object string to the stringValue to keep it from getting released.

-(void) textDidEndEditing:(NSNotification *)notification {
    [NSEvent removeMonitor:eventMonitor];
    eventMonitor = nil;

    NSTextView *textView=[notification object];
    self.stringValue=textView.string;
}
user3404855
  • 215
  • 1
  • 9
Scott Jenkins
  • 321
  • 2
  • 7
  • it works like a charm. I just put the remove event call in the resignFirstResponder method to mirror the adding placement – Jim75 Mar 19 '19 at 08:51
5

You can subclass NStextField and use keyUp that works for NSTextField.

in .h

@interface MyTextField : NSTextField <NSTextFieldDelegate>

in .m

-(void)keyUp:(NSEvent *)theEvent {
    NSLog(@"Pressed key in NStextField!");
}
Justin Boo
  • 10,132
  • 8
  • 50
  • 71
-3

Add UITextFieldDelegate to your .h like this

@interface ViewController :  UIViewController <UITextFieldDelegate> {

Then you can use this to detect every key press in a text field

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

Return YES to allow the character that was pressed to be inserted into the field but you can add whatever code you need in here.

Darren
  • 10,182
  • 20
  • 95
  • 162