NSMenuItem KeyEquivalent""(空格)错误 [英] NSMenuItem KeyEquivalent " "(space) bug

查看:363
本文介绍了NSMenuItem KeyEquivalent""(空格)错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要设置等效键"(空格)没有NSMenuItem的任何修饰符(在应用程序主菜单中).

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

文档中的内容如下:

例如,在播放媒体的应用程序中,播放"命令可能仅映射到"(空格),而没有命令键.您可以使用以下代码执行此操作:

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 setKeyEquivalent:@" "];

[menuItem setKeyEquivalentModifierMask:0];

[menuItem setKeyEquivalentModifierMask:0];

键等效项设置成功,但是不起作用.当我按空格"时,没有修饰符的键没有任何反应,但是当我按下"Space"键时与"Fn"一起修改键.

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!

推荐答案

我遇到了同样的问题.我还没有进行非常认真的研究,但是据我所知,空格键并不像可可"的键盘快捷键那样看起来",因此它被路由到-insertText:.我的解决方案是对NSWindow进行子类化,在响应程序链上捕获它(大概可以对NSApp进行子类化),然后将其显式发送到菜单系统:

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]
                                         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];
    }
}

这篇关于NSMenuItem KeyEquivalent""(空格)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆