Alex Rivera | Logout

Capturing "up" and "down" keys pressed in NSTextField

Asked 2011-07-11T09:30:03.923
11

I wrote an "Agent" Cocoa app where a TextField is in the status icon's menu. The icon menu is displayed as followed:


(source: ifotos.pl)

The stepper increments or decrements the value by 5. The purpose of the text field is to press the "up" or "down" keys which achieves the same result of the value being incremented or decremented by 5.

I have found out that following code must be used - (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector of NSControlTextEditingDelegate protocol.

The problem I am currently encountering is how to implement the code. My normal approach is use sth.delegate = self;. I attempted to search for an answer but the replies consisted of using to control:textView:.... I was unable to learn how to use the delegate method even with further research.

Thank you in advance!

Edit
Report

1 Answer

2

Try overriding NSResponder message like this:

- (BOOL)performKeyEquivalent:(NSEvent *)theEvent{
    switch ([[theEvent charactersIgnoringModifiers] characterAtIndex:0]) {
        case NSUpArrowFunctionKey:
            // Increase by 5 here
            return YES;
            break;
        case NSDownArrowFunctionKey:;
            // Decrease by 5 here
            return YES;
            break;
        default:
            break;
    }
    return [super performKeyEquivalent:theEvent];
}
answered 2011-07-11T12:43:24.880

Your Answer