On Oct 20, 2008, at 2:07 PM, Sean McBride wrote:

Hi all,

In 10.5, NSTableView handles many keypresses automatically: up arrow,
down arrow, page up, page down, home, end, and even 'type select'.

I also need to (robustly!) support the 'delete' key.

Overriding deleteBackward: (from NSResponder) doesn't seem to work. For
some reason, it is never called.

For various reasons, NSTableView doesn't implement - interpretKeyEvents:, that's why it isn't called.




What is the most correct way to support this?  I need the keypresses
that NSTableView already supports to continue working, and I need to
support custom key bindings (meaning the delete functionality may not be
mapped to the delete key).

My current best is the following, but I don't think it will support
custom keybindings.

- (void)keyDown:(NSEvent*)event
{
        BOOL deleteKeyEvent = NO;

        if ([event type] == NSKeyDown)
        {
                NSString* pressedChars = [event characters];
                if ([pressedChars length] == 1)
                {
                        unichar pressedUnichar =
   [pressedChars characterAtIndex:0];
                        
                        if ( (pressedUnichar == NSDeleteCharacter) ||
   (pressedUnichar == 0xf728) )
                        {
                                deleteKeyEvent = YES;
                        }
                }
        }
        
// If it was a delete key, handle the event specially, otherwise call super.
        if (deleteKeyEvent)
        {
                // This will end up calling deleteBackward: or deleteForward:.
                [self interpretKeyEvents:[NSArray arrayWithObject:event]];
        }
        else
        {
                [super keyDown:event];
        }
}

This will work well, and is the only good way to implement it. Ideally, you would only want to do this if there isn't an active "type select" going on, but there is no API to tell if that is happening or not. (As usual, feel free to log requests for that). Theoretically, backspacing while type selecting could undo the last character; in practice, that is a dangerous thing to do, since you might accidentally delete something you didn't intend to.

corbin


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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