NSMenuItem KeyEquivalent Error "" (space)

I want to set the equivalent of the key "" (space) without any modifiers for NSMenuItem (in the main menu of the application).

As follows from the documentation:

For example, in a media playback application, a playback command can only be displayed in "(space) without a command key. You can do this with the following code:

[menuItem setKeyEquivalent: @ ""];

[menuItem setKeyEquivalentModifierMask: 0];

Key equivalent sets are successful, but this does not work. When I press the spacebar without modifiers, nothing happens, but it works when I press the spacebar with the modifier key Fn.

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

+6
source share
3 answers

I had the same problem. I didn’t do much research, but as far as I can tell, the space does not “look” like a keyboard shortcut with Cocoa, so it redirects to -insertText: My solution was to subclass NSWindow, catch it as you move up the defendant chain (presumably you could subclass NSApp) and send it explicitly to the menu system:

 - (void)insertText:(id)insertString { if ([insertString isEqual:@" "]) { NSEvent *fakeEvent = [NSEvent keyEventWithType:NSKeyDown location:[self mouseLocationOutsideOfEventStream] modifierFlags:0 timestamp:[[NSProcessInfo processInfo] systemUptime] windowNumber:self.windowNumber context:[NSGraphicsContext currentContext] characters:@" " charactersIgnoringModifiers:@" " isARepeat:NO keyCode:49]; [[NSApp mainMenu] performKeyEquivalent:fakeEvent]; } else { [super insertText:insertString]; } } 
+3
source

I just had the same twist problem ...

The space key equivalent works fine in my application, and the associated NSMenuItem IBAction is in the application delegate.

If I translate IBAction into a dedicated controller, it fails. All other equivalents of the menu keys continue to work, but the spacebar does not respond (this is normal with the modifier key, but unmodified @ "" does not work).

I tried various workarounds, such as binding directly to the controller and binding through a chain of responders, but to no avail. I tried the code:

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

and the way to build the interface, the behavior is the same

I tried to subclass NSWindow, like Justin's answer, but so far have not been able to get this to work.

So, so far I have given up and moved this IBAction to the App Delegate, where it works. I don’t see this as a solution, I just do it ... maybe it’s a mistake, or (rather) I just don’t understand the event messages and the responder chain is good enough.

+1
source

At the top of this post, because I need to use space too, but none of these solutions work for me.

So, I'm a subclass of NSApplication and use the sendEvent: selector using justin k solution :

 - (void)sendEvent:(NSEvent *)anEvent { [super sendEvent:anEvent]; switch ([anEvent type]) { case NSKeyDown: if (([anEvent keyCode] == 49) && (![anEvent isARepeat])) { NSPoint pt; pt.x = pt.y = 0; NSEvent *fakeEvent = [NSEvent keyEventWithType:NSKeyDown location:pt modifierFlags:0 timestamp:[[NSProcessInfo processInfo] systemUptime] windowNumber: 0 // self.windowNumber context:[NSGraphicsContext currentContext] characters:@" " charactersIgnoringModifiers:@" " isARepeat:NO keyCode:49]; [[NSApp mainMenu] performKeyEquivalent:fakeEvent]; } break; default: break; } } 

Hope this helps

+1
source

Source: https://habr.com/ru/post/918746/


All Articles