Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override keydownevent in NSTextfield

I made a subclass of the nstextfield and i override the keydown event but my code doesn't work, then i override de keyup event and the code works perfectly. My keydown code(doesn't work):

-(void)keyDown:(NSEvent *)event {
    NSLog(@"Key released: %hi", [event keyCode]);

    if ([event keyCode]==125){

        [[self window] selectKeyViewFollowingView:self];
    }
    if ([event keyCode]==126){

        [[self window] selectKeyViewPrecedingView:self];
    }

}

My keyup code (it works):

-(void)keyUp:(NSEvent*)event
{if ([event keyCode]==125){

        [[self window] selectKeyViewFollowingView:self];
    }
    if ([event keyCode]==126){

        [[self window] selectKeyViewPrecedingView:self];
    }

    if ([event keyCode]==36){

        [[self window] selectKeyViewFollowingView:self];
    }

    }

I don't see where is the problem with my keydown code. Any suggest will be accepted

EDIT: I have read that you have to subclass NSTextView instead of NSTextField.

like image 969
Javier Beltrán Avatar asked May 06 '11 17:05

Javier Beltrán


4 Answers

You can do it without subclassing by using NSTextFieldDelegate methods:

As @Darren Inksetter said, you can use control:textView:doCommandBySelector:

First, declare NSTextFieldDelegate in your interface tag. Then implement the method:

- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
{
    if( commandSelector == @selector(moveUp:) ){
        // Do yourthing here, like selectKeyViewFollowingView
        return YES;    // Return YES means don't pass it along responders chain. Return NO if you still want system's action on this key.
    }
    if( commandSelector == @selector(moveDown:) ){
        // Do the same with the keys you want to track
        return YES;
    }
    return NO;
}
like image 91
anhdat Avatar answered Nov 09 '22 16:11

anhdat


The keydown event can't be overriden in a NSTextField, if you want , you could override the keydown event of the super view or you could use a NSTextView or just override the keyup event in the NSTextField

like image 37
Javier Beltrán Avatar answered Nov 09 '22 17:11

Javier Beltrán


You will want to look at

control:textView:doCommandBySelector:

http://developer.apple.com/library/mac/#documentation/cocoa/reference/NSControlTextEditingDelegate_Protocol/Reference/Reference.html

like image 4
Darren Inksetter Avatar answered Nov 09 '22 16:11

Darren Inksetter


Swift 5 example.


    func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
        switch commandSelector {
        case #selector(moveUp(_:)):
            impl.tableView.doCommand(by: commandSelector)
            return true
        case #selector(moveDown(_:)):
            impl.tableView.doCommand(by: commandSelector)
            return true
        default: return false
        }
    }

like image 4
eonil Avatar answered Nov 09 '22 15:11

eonil