检测 Ctrl + Return 或 Ctrl +输入按键

发布于 2024-12-03 21:27:05 字数 707 浏览 1 评论 0原文

我发现很难找到关于此事的任何官方信息。

我有一个 TextView 并覆盖 keyDown 事件,并尝试检测用户是否按下 Ctrl + Enter

- (void)keyDown:(NSEvent *)theEvent
{
    if([theEvent modifierFlags] & NSControlKeyMask && /* how to check if enter is pressed??? */)
    {
        NSLog(@"keyDown: ctrl+enter");
        if(_delegate)
        {
            if([_delegate respondsToSelector:@selector(didSomething:)])
            {
                [_delegate performSelector:@selector(didSomething:) withObject:nil];
            }
        }
    }else
    {
        [super keyDown:theEvent];
    }
}

我尝试过不同的方法,但没有任何效果。

有人吗?

(我很抱歉问这么一个微不足道的问题,但我已经用谷歌搜索了一段时间,但没有找到任何东西)

I find it very hard to find anything official on this matter.

I have a TextView and override the keyDown event and try to detect if the user pressed Ctrl + Enter.

- (void)keyDown:(NSEvent *)theEvent
{
    if([theEvent modifierFlags] & NSControlKeyMask && /* how to check if enter is pressed??? */)
    {
        NSLog(@"keyDown: ctrl+enter");
        if(_delegate)
        {
            if([_delegate respondsToSelector:@selector(didSomething:)])
            {
                [_delegate performSelector:@selector(didSomething:) withObject:nil];
            }
        }
    }else
    {
        [super keyDown:theEvent];
    }
}

I have tried different things but nothing worked.

anyone?

(i'm sorry to ask such a trivial question but i have googled for a while now and haven't found anything)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

杀手六號 2024-12-10 21:27:05
unichar c = [theEvent charactersIgnoringModifiers] characterAtIndex:0];

if(([theEvent modifierFlags] & NSControlKeyMask) && (c == NSCarriageReturnCharacter || c == NSEnterCharacter) {
    // do stuff
}

或者,您可以使用委托的 textView:doCommandBySelector:

- (BOOL)textView:(NSTextView *)aTextView doCommandBySelector:(SEL)aSelector {
    // insertLineBreak: is the method sent when a user presses control-return in a text view
    if (aSelector == @selector(insertLineBreak:)) {
            // do stuff
            return YES;
        }
    }

    return NO;
}
unichar c = [theEvent charactersIgnoringModifiers] characterAtIndex:0];

if(([theEvent modifierFlags] & NSControlKeyMask) && (c == NSCarriageReturnCharacter || c == NSEnterCharacter) {
    // do stuff
}

Alternatively, you can use the delegate's textView:doCommandBySelector::

- (BOOL)textView:(NSTextView *)aTextView doCommandBySelector:(SEL)aSelector {
    // insertLineBreak: is the method sent when a user presses control-return in a text view
    if (aSelector == @selector(insertLineBreak:)) {
            // do stuff
            return YES;
        }
    }

    return NO;
}
姜生凉生 2024-12-10 21:27:05

您可以覆盖 键盘操作(插入换行符),当 ctrl-return 时发送在文本视图中输入:

- (void)insertLineBreak:(id)sender {
    NSLog(@"ctrl-return");

    if(_delegate)
    {
        …
    }
}

Instead of overriding -keyDown:, you could override the keyboard action (insert a line break) that’s sent when ctrl-return is typed in a text view:

- (void)insertLineBreak:(id)sender {
    NSLog(@"ctrl-return");

    if(_delegate)
    {
        …
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文