3

i have an NSCollectionView in my application's main window that manages a collection of custom NSView items. Each custom view has a context menu assigned to it. I want to add shortcut keys to some of the items, for example to associate a "delete" key with "remove item from collection" action. I've added key equivalents to context menu items through IB but the question is how do i make the collection items respond to the pressed keys?

I know that i can achieve this by adding this menu to the NSApp's main menu and keep track of the selected item. Is there any other way besides that?

Monolo
  • 18,205
  • 17
  • 69
  • 103
Inso Reiges
  • 1,889
  • 3
  • 17
  • 30

1 Answers1

0

You could add something like this to your NSCollectionView subclass:

- (BOOL)performKeyEquivalent:(NSEvent *)theEvent
{
    BOOL rv = NO;

    id firstResponder = self.window.firstResponder;

    if ([firstResponder isKindOfClass:[NSView class]] && [firstResponder isDescendantOf:self]) {
        // Note: performKeyEquivalent: messages come DOWN the view hierarchy, not UP the responder chain.

        // Perform the key equivalent

    }

    if (!rv) {
        rv = [super performKeyEquivalent:theEvent];
    }

    return rv;
}
Steve Shepard
  • 221
  • 2
  • 5