Getting a NSMenuItem to respond to two different key equivalents

  • Notice how both Mail and Safari let you use cmd-+ and cmd-= to make
    text larger, but the menu item just shows cmd-+?

    Yeah, how do I do that?

    The NIBs are *not* using the nifty alternate item feature because you
    can't do that with only shift, it seems (can't even check it off in
    IB).  I tried mangling in validateMenuItem but that's not called
    unless the menu is being displayed, it seems.  So where can I flip
    the keyEquivalent as this happens, if that's what's happening (the
    simplest solution).

    Adam Knight
    codepoetry - http://www.codepoetry.net/products/
  • > The NIBs are *not* using the nifty alternate item feature because
    > you can't do that with only shift, it seems (can't even check it off
    > in IB).  I tried mangling in validateMenuItem but that's not called
    > unless the menu is being displayed, it seems.  So where can I flip
    > the keyEquivalent as this happens, if that's what's happening (the
    > simplest solution).

    You can check for the alternate in -[NSApplication sendEvent:] and
    call super with a synthetic event whose characters are changed to
    match the equivalent on the menu item. That has the benefit of
    invoking all the standard mechanisms for matching key equivalents and
    highlighting the menu item.

    -Ben
  • Cant it be done in the first responders keydown method?

    Regards,
    Dominik

    Am 12.10.2007 um 00:34 schrieb Benjamin Stiglitz:

    >> The NIBs are *not* using the nifty alternate item feature because
    >> you can't do that with only shift, it seems (can't even check it
    >> off in IB).  I tried mangling in validateMenuItem but that's not
    >> called unless the menu is being displayed, it seems.  So where can
    >> I flip the keyEquivalent as this happens, if that's what's
    >> happening (the simplest solution).
    >
    >
    > You can check for the alternate in -[NSApplication sendEvent:] and
    > call super with a synthetic event whose characters are changed to
    > match the equivalent on the menu item. That has the benefit of
    > invoking all the standard mechanisms for matching key equivalents
    > and highlighting the menu item.
    >
    > -Ben
  • > Cant it be done in the first responders keydown method?

    That gets called after equivalent matching, so it’ll be too late.

    -Ben
  • Am 12.10.2007 um 00:13 schrieb Adam Knight:
    > The NIBs are *not* using the nifty alternate item feature because
    > you can't do that with only shift, it seems (can't even check it
    > off in IB).  I tried mangling in validateMenuItem but that's not
    > called unless the menu is being displayed, it seems.  So where can
    > I flip the keyEquivalent as this happens, if that's what's
    > happening (the simplest solution).

      Maybe I'm mixing this up with Carbon, but I think there's a way to
    create an invisible menu. You could do that, put a second menu item
    with the secondary shortcut in there, and hook that up just like the
    first one.

      Though, keep in mind that different keyboards have different
    characters sharing a key. E.g. on a German keyboard, plus isn't on a
    shifted key, and Plus with shift is the asterisk. OTOH, we usually do
    what you're doing for the help menu item, because the question mark
    is Shift-ß (German SZ ligature), so you can usually leave out the
    shift key and still trigger the help menu item.

    Cheers,
    -- M. Uli Kusterer
    http://www.zathras.de
  • For the sake of the archives, here's what worked (thanks!).  I added
    the following to a NSApplication subclass and updated my Info.plist
    accordingly (note characters and charactersIgnoringModifiers):

    - (void)sendEvent:(NSEvent *) event
    {
      if ([event type] == NSKeyDown) {
        if( (([event modifierFlags] & NSCommandKeyMask) ==
    NSCommandKeyMask) && [[event charactersIgnoringModifiers]
    isEqualToString:@"="] ) {
          event = [NSEvent keyEventWithType:[event type] location:[event
    locationInWindow] modifierFlags:[event modifierFlags] timestamp:
    [event timestamp] windowNumber:[event windowNumber] context:[event
    context] characters:@"=" charactersIgnoringModifiers:@"+" isARepeat:
    [event isARepeat] keyCode:[event keyCode]];
        }
      }

      [super sendEvent:event];
    }

    Adam Knight
    Notae: Organize your notes, organize your life - http://
    www.codepoetry.net/products/notae/

    On Oct 11, 2007, at 5:34 PM, Benjamin Stiglitz wrote:

    >> The NIBs are *not* using the nifty alternate item feature because
    >> you can't do that with only shift, it seems (can't even check it
    >> off in IB).  I tried mangling in validateMenuItem but that's not
    >> called unless the menu is being displayed, it seems.  So where can
    >> I flip the keyEquivalent as this happens, if that's what's
    >> happening (the simplest solution).
    >
    >
    > You can check for the alternate in -[NSApplication sendEvent:] and
    > call super with a synthetic event whose characters are changed to
    > match the equivalent on the menu item. That has the benefit of
    > invoking all the standard mechanisms for matching key equivalents
    > and highlighting the menu item.
    >
    > -Ben
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today