0
votes

I'm trying to detect the enter key from a custom NSTextView (from a NSTextField completion list) but I can't get it to work.

My interface:

@interface PaddedTextView : NSTextView < NSTextViewDelegate >

I implement:

- (BOOL)textView:(NSTextView *)textView doCommandBySelector (SEL)commandSelector
    {

But it is never called.

The Completion list disappears when I press enter after selecting an option.

I have also implemented:

- (void)keyDown:(NSEvent *)event

and it will detect alpha numeric key events but not the enter key.

I also have implemented flagsChanged to capture the shift key and that works.

I am using a custom NSTextField, NSTextFieldCell and NSTextView.

When I select a completion entry and hit the enter key the value will be inserted into the NSTextField control but I want to perform an action when this happens and I can't detect it...

Any help is appreciated. Thanks

2

2 Answers

0
votes

I found a solution to my problem. (Update: This doesn't work)

I added the following to my NSTextView implementation:

-(void) insertCompletion:(NSString *)word forPartialWordRange:(NSRange)charRange movement:(NSInteger)movement isFinal:(BOOL)flag {
    if(movement == NSReturnTextMovement){
        NSLog(@"Enter key pressed...");
    }
    [super insertCompletion:word forPartialWordRange:charRange movement:movement isFinal:flag];
}

Update: Actually this doesn't always work. This function is only called when a completion option is selected. If you type in the text field and hit enter I can't detect that key event, it only dismisses the completion list. Still looking for a real solution.

-1
votes

I'm not sure about the completion list implementation, However at least on a normal NSTextView, insertNewline:(id)sender will be invoked when enter key is pressed.