On Jul 7, 2008, at 5:07 PM, Graham Cox wrote:
The view that is first responder needs to override -keyDown: and do this:

        [self interpretKeyEvents:[NSArray arrayWithObject:event]];

which hooks the event into the standard dispatcher for these methods.

(One thing that has long puzzled me about this though - why is the parameter an array of events when only one event is ever available?)


Okay, got this message late though it looks like another had responded to it already. Given that I need to override -keyDown: (or - performKeyEquivalent:) in applicable first responders anyway, and it doesn't really read well to have -forwardDelete:, backwardDelete:, etc. actions, I just did the following:

#pragma mark Keyboard events

- (BOOL)acceptsFirstResponder {
        return YES;
}

- (void)keyDown:(NSEvent*)keyEvent {
        NSString* keyCharacters = [keyEvent charactersIgnoringModifiers];
        if ([keyCharacters length] == 1) {
                unichar keyPressed = [keyCharacters characterAtIndex:0];
if (keyPressed == NSDeleteCharacter || keyPressed == NSDeleteCharFunctionKey) {
                        [self doCommandBySelector:@selector(delete:)];
                }
                else [super keyDown:keyEvent];
        }
        else [super keyDown:keyEvent];
}

Provided the view(s) with this code is the first responder, it causes the action to fire when either delete key is pressed, with any modifiers. If I need more fine-grained behaviour (forceDelete: or something when Cmd is pressed), I'll just add it in that method.

I'm tempted to improve upon this by using the NSUserInterfaceValidations informal protocol (which my app delegate already conforms to for the sake of the Edit>Delete menu item) to check if the delete action is even performed. The way this code works, the delete key gets swallowed even when nothing is deleted. This silent dropping of the delete key is how many apps behave, but wouldn't it be better to pass the event up to super when the delete: action isn't really valid, so it can eventually result in a beep/bonk/ blip?

thanks,
-natevw
_______________________________________________

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