To intercept key presses in a way that they can still be filtered out, various NSResponder
messages may be overwritten, such as keyDown:
or interpretKeyEvents:
.
To be able to do that, a subclass of a NSTextView
needs to be used as the field editor. For that, one subclasses NSTextFieldCell
and overrides fieldEditorForView:
, returning the subclass (see Custom field editor for NSTextFieldCell in an NSTableView).
Here's the relevant code excerpts:
In a subclassed NSTextFieldCell
(which then has to be assigned in Interface Builder for the editable column, or returned by the NSTableViewDelegate
's dataCellForTableColumn
message):
- (NSTextView *)fieldEditorForView:(NSView *)aControlView
{
if (!self.myFieldEditor) {
self.myFieldEditor = [[MyTextView alloc] init];
self.myFieldEditor.fieldEditor = YES;
}
return self.myFieldEditor;
}
It also requires the declaration of a property in the @interface
section:
@property (strong) MyTextView *myFieldEditor;
And then in MyTextView
, which is a subclass of NSTextView
:
-(void)keyDown:(NSEvent *)theEvent
{
NSLog(@"MyTextView keyDown: %@", theEvent.characters);
static bool b = true;
if (b) { // this silly example only lets every other keypress through.
[super keyDown:theEvent];
}
b = !b;
}