Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get keyDown event for an NSTextField

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 !

like image 350
Laurent Crivello Avatar asked Jan 15 '12 08:01

Laurent Crivello


2 Answers

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;
}
like image 109
Scott Jenkins Avatar answered Oct 08 '22 21:10

Scott Jenkins


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!");
}
like image 21
Justin Boo Avatar answered Oct 08 '22 22:10

Justin Boo