本文介绍了检测Ctrl + Return或Ctrl + Enter按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在这件事上,我很难找到任何官方的东西.
I find it very hard to find anything official on this matter.
我有一个TextView并覆盖keyDown事件,并尝试检测用户是否按下了 + .
I have a TextView and override the keyDown event and try to detect if the user pressed + .
- (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.
有人吗?
(很抱歉提出这样一个琐碎的问题,但是我已经在Google上搜索了一段时间了,但是没有找到任何东西)
(i'm sorry to ask such a trivial question but i have googled for a while now and haven't found anything)
推荐答案
unichar c = [theEvent charactersIgnoringModifiers] characterAtIndex:0];
if(([theEvent modifierFlags] & NSControlKeyMask) && (c == NSCarriageReturnCharacter || c == NSEnterCharacter) {
// do stuff
}
或者,您可以使用委托人的textView:doCommandBySelector:
:
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;
}
这篇关于检测Ctrl + Return或Ctrl + Enter按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!