Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

How to accept keyboard input in a menubar app?

ChrisYatesChrisYates Posts: 325Registered Users
I have created a menubar app and it all works fine but what I can't seem to figure out is how to allow the user to use the keyboard to enter numbers into the menubar app, I've tried to set the keyboard equivalent in IB but it just ignores the input?

I'm hazarding a guess that it might have something to do with setting the focus on the app when it's in view?

Essentially it's just a calculator so I wish to allow the user to use their number keypad to do the calculations.

Any help would be much appreciated.

Replies

  • nestochinestochi Posts: 44Registered Users
    Are you using a Popover?
    there is a known bug in Lion with Popovers in menu bars not accepting keyboard input.
  • ChrisYatesChrisYates Posts: 325Registered Users
    Thanks nestochi. No I'm not using a popover. I have now got it to work partially! What I did was create a separate menu and allocate numbers to the menu and added key equivalents to the numbers. The problem I am having now is it only works when the app is front most but the calculator is not showing! If I click on the menubar icon for my app so that the window drops down to show the calculator the key equivalents get ignored. As soon as the window disappears the key equivalents work again?

    I thought this might be because it recognises the drop down window as a menu so will ignore any other menu and it's keyboard equivalents so I tried a test with Safari. If I click on the 'File Menu' in Safari and then use an 'Edit Menu' keyboard equivalent such as 'Select All' it responds perfectly even though the 'File Menu' is open.

    This let's me know it can be done but I have no idea why it's ignoring my keyboard equivalents when the app is showing.
  • slahteineslahteine Posts: 196New Users
    Standard behavior in menus is to treat bare keys as a shortcut to select menu items matching what you type. I haven't tried to override this behavior, but I'd expect this NSMenuDelegate method to be the first step:

    - (BOOL)menuHasKeyEquivalent:(NSMenu *)menu forEvent:(NSEvent *)event target:(id *)target action:(SEL *)action

    The menu receives your keyboard event, and you return YES for your bare keys. Then in your NSMenu subclass you would implement - (BOOL)performKeyEquivalent:(NSEvent *)theEvent to actually handle the key.

    If that's what you're already doing, then I'm not sure what to try next!
    | I wrote ChordCalc, A fretboard 'calculator' for iOS and FretPet, a guitar-oriented music sequencer for OS X.
Sign In or Register to comment.