Re: performKeyEquivalent & key events
Re: performKeyEquivalent & key events
- Subject: Re: performKeyEquivalent & key events
- From: Dustin Voss <email@hidden>
- Date: Mon, 16 Feb 2004 16:54:18 -0800
On 16 Feb, 2004, at 11:02 AM, Chad Armstrong wrote:
I have a list of items in a table, and when a user hits the Delete or
Backspace key, I want the highlighted items in the list to disappear.
What would be the best way to implement this? I am assuming that I
will need to use performKeyEquivalent at some point. I also have a
button which can be clicked to delete the items from the list, but
that also has another key binding, other than Delete or Backspace.
I handled this by creating an NSResponder subclass called
DeleteInterceptor which calls a delegate method when it recognizes a
forward or backward delete. I instantiate it in my awakeFromNib method,
and it adds itself to the responder chain above the table view.
The source code is short, and here it is:
-----------------------------------------------------------
@interface DeleteInterceptor : NSResponder
{
id theTarget;
SEL theSelector;
id thePrevResponder;
}
- (id) initWithSelector:(SEL)sel onTarget:(id)targ above:(NSResponder
*)resp;
- (void) dealloc;
- (void) addToChainAbove:(NSResponder *)responder;
- (void) removeFromChain;
@end
------------------------------------------------------------
@implementation DeleteInterceptor
- (id) initWithSelector:(SEL)sel onTarget:(id)targ above:(NSResponder
*)resp
{
self = [super init];
if (self == nil) return nil;
theSelector = sel;
theTarget = targ;
[self addToChainAbove:resp];
return self;
}
- (void) dealloc
{
[self removeFromChain];
[super dealloc];
}
- (void) addToChainAbove:(NSResponder *)responder
{
if (responder != thePrevResponder)
{
[self removeFromChain];
NSResponder *nextResp = [responder nextResponder];
thePrevResponder = [responder retain];
[responder setNextResponder:self];
[self setNextResponder:nextResp];
}
}
- (void) removeFromChain
{
[thePrevResponder setNextResponder:[self nextResponder]];
[self setNextResponder:nil];
[thePrevResponder release];
thePrevResponder = nil;
}
- (void) keyDown:(NSEvent *)theEvent
{
[self interpretKeyEvents:[NSArray arrayWithObject:theEvent]];
}
- (void) deleteBackward:(id)sender
{
// TODO: Add more safety.
[theTarget performSelector:theSelector withObject:sender];
}
- (void) deleteForward:(id)sender
{
// TODO: Add more safety.
[theTarget performSelector:theSelector withObject:sender];
}
@end
--------------------------------------------------------------
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.