On Wed, 21 Apr 2010 00:15:44 -0700, Chris Idou said:

>What would be the appropriate way to have an NSTableView notice that you
>hit the delete key and delete the current row?

My preferred solution is the following:
- subclass NSTableView
- add the following outlet:
        IBOutlet NSControl* deleteControl;
- connect the tableview's deleteControl outlet to the push button that
also performs delete (you should have such a button so that
deleteability is discoverable)
- add the following code.  This way, pressing the delete key will blink
the delete button and perform the same action as that button.

- (void)deleteSelection
{
        // Simulate a mouse click in the control.  The control is expected to
perform some kind of "delete" function.
        [deleteControl performClick:self];
}

- (void)deleteBackward:(id)inSender
{
        [self deleteSelection];
}

- (void)deleteForward:(id)inSender
{
        [self deleteSelection];
}

- (void)keyDown:(NSEvent*)event
{
        BOOL deleteKeyEvent = NO;
        
        // Check if the event was a keypress that matches either the backward
or forward delete key.
        if ([event type] == NSKeyDown)
        {
                NSString* pressedChars = [event characters];
                if ([pressedChars length] == 1)
                {
                        unichar pressedUnichar = [pressedChars 
characterAtIndex:0];
                        
                        // Test the key that was pressed. Note that this does 
not work with
custom key bindings. Ideally, NSTableView should support the delete keys
itself <rdar://6305317>.
                        if ( (pressedUnichar == NSDeleteCharacter) || 
(pressedUnichar ==
NSDeleteFunctionKey) )
                        {
                                deleteKeyEvent = YES;
                                
                                // Additionally, it would be ideal to be able 
to check if 'type
select' is in progress and if so not treat this as a delete. The user
may expect the delete key to delete the last keypress of this type
select sequence. Type select does not work that way, but he might expect
it nonetheless, and we shouldn't delete his data in this case. No such
API exists: <rdar://6305086>.
                        }
                }
        }
        
        // If it was a delete key, handle the event specially, otherwise call
super. In general, we want super to handle most keypresses since it
handles arrow keys, home, end, page up, page down, and type select.
        if (deleteKeyEvent)
        {
                // This will end up calling deleteBackward: or deleteForward:.
                [self interpretKeyEvents:[NSArray arrayWithObject:event]];
        }
        else
        {
                [super keyDown:event];
        }
}

Thanks to Corbin Dunn for 90% of this solution!

--
____________________________________________________________
Sean McBride, B. Eng                 [email protected]
Rogue Research                        www.rogue-research.com
Mac Software Developer              Montréal, Québec, Canada


_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to