Take the 2-minute tour ×

I want to set key equivalent " "(space) without any modifiers for NSMenuItem (in App Main Menu).

As follows from documentation:

For example, in an application that plays media, the Play command may be mapped to just “ ” (space), without the command key. You can do this with the following code:

[menuItem setKeyEquivalent:@" "];

[menuItem setKeyEquivalentModifierMask:0];

Key Equivalent sets successfully, but it don't work. When I press "Space" key without modifiers nothing happens, but it's works when i press "Space" with "Fn" modifier key.

I need to use "Space" without modifiers. Any help please!

share|improve this question

2 Answers 2

up vote 1 down vote accepted

I had the same problem. I haven't investigated very hard, but as far as I can tell, the spacebar doesn't "look" like a keyboard shortcut to Cocoa so it gets routed to -insertText:. My solution was to subclass the NSWindow, catch it as it goes up the responder chain (presumably you could subclass NSApp instead), and send it off to the menu system explicitly:

- (void)insertText:(id)insertString
    if ([insertString isEqual:@" "]) {
        NSEvent *fakeEvent = [NSEvent keyEventWithType:NSKeyDown
                                              location:[self mouseLocationOutsideOfEventStream]
                                             timestamp:[[NSProcessInfo processInfo] systemUptime]
                                               context:[NSGraphicsContext currentContext]
                                            characters:@" "
                           charactersIgnoringModifiers:@" "
        [[NSApp mainMenu] performKeyEquivalent:fakeEvent];
    } else {
        [super insertText:insertString];
share|improve this answer
Thanks for you answer, it works) –  Kira Aug 7 '12 at 12:22

I have just been experiencing the same problem with a twist...

The spacebar key equivalent works fine in my app while the NSMenuItem's linked IBAction is located in the App Delegate.

If I move the IBAction into a dedicated controller it fails. All other menu item key equivalents continue to work but the spacebar does not respond (it is ok with a modifier key, but unmodified @" " will not work).

I have tried various workarounds, like linking directly to the controller vs. linking via the responder chain, to no avail. I tried the code way:

[menuItem setKeyEquivalent:@" "];  
[menuItem setKeyEquivalentModifierMask:0];  

and the Interface Builder way, the behaviour is the same

I have tried subclassing NSWindow, as per Justin's answer, but so far have failed to get that to work.

So for now I have surrendered and relocated this one IBAction to the App Delegate where it works. I don't regard this as a solution, just making do... perhaps it's a bug, or (more likely) I just don't understand event messaging and the responder chain well enough.

share|improve this answer

Your Answer


