I have been struggling for some time to find how to get a keypress in NSOutlineView into my Controller.
There seem to be many others asking similar questions so I decided document my solution which may help.
I created a class M
M
If the delegate handles the key it should return YES, otherwise NO.
// MyOutlineView.h #import <Cocoa/Cocoa.h>
@protocol MyOutlineViewDelegate @optional -(BOOL) keyPressedInOutlineView:(unichar) character; @end
@interface MyOutlineView : NSOutlineView {
NSObject
@end
// MyOutlineView.m #import “MyOutlineView.h”
@implementation MyOutlineView @synthesize keyDelegate;
-(void)keyDown:(NSEvent *)theEvent { unichar character = theEvent characters] characterAtIndex:0]; if([self.keyDelegate respondsToSelector:@selector(keyPressedInOutlineView:)]) if([self.keyDelegate keyPressedInOutlineView:character]) return; [super keyDown:theEvent]; }
@end
In Interface Builder change the class of the O
-(BOOL)keyPressedInOutlineView:(unichar) character { [[NSLog (@”Key in Controller! %C”, character); if (character == 0x0d) { NSLog (@”Return in OutlineView!”); return YES; } return NO; }