1
我需要一个特殊的文本字段,应该做到以下东西:当输入键被按下使用什么对象的特殊文本编辑
- 多
- tab键支持
- 发送动作
- Alt + Enter新行
- SHIFT +新线进入
我不知道该用什么。
NSTextView看起来不错,但我不能设置上输入一个动作,在按下一个新行
的NSTextField回车键结果已经没有标签,关键支撑和转向输入不工作。
有什么想法?谢谢!
我需要一个特殊的文本字段,应该做到以下东西:当输入键被按下使用什么对象的特殊文本编辑
我不知道该用什么。
NSTextView看起来不错,但我不能设置上输入一个动作,在按下一个新行
的NSTextField回车键结果已经没有标签,关键支撑和转向输入不工作。
有什么想法?谢谢!
最好的办法是继承NSTextView
以获得所需的功能。下面是一个简单的例子:
MyTextView.h
@interface MyTextView : NSTextView
{
id target;
SEL action;
}
@property (nonatomic, assign) id target;
@property (nonatomic, assign) SEL action;
@end
MyTextView.m
@implementation MyTextView
@synthesize target;
@synthesize action;
- (void)keyDown:(NSEvent *)theEvent
{
if ([theEvent keyCode] == 36) // enter key
{
NSUInteger modifiers = [theEvent modifierFlags];
if ((modifiers & NSShiftKeyMask) || (modifiers & NSAlternateKeyMask))
{
// shift or option/alt held: new line
[super insertNewline:self];
}
else
{
// straight enter key: perform action
[target performSelector:action withObject:self];
}
}
else
{
// allow NSTextView to handle everything else
[super keyDown:theEvent];
}
}
@end
设置目标和行动将完成如下:
[myTextView setTarget:someController];
[mytextView setAction:@selector(omgTheUserPressedEnter:)];
有关授权码和NSResponder
消息像insertNewline:
全套更多详细信息,请参阅优秀的答案,我的问题有关NSEvent
键码:Where can I find a list of key codes for use with Cocoa's NSEvent class?
从你的描述,你应该只需要重写insertNewline:并添加动作和目标属性。在insertNewline:中,如果正在按住shift或option,则调用超级实现,如果不是,则执行该操作。 – ughoavgfhw 2010-12-19 01:02:51
谢谢你看起来不错。我想知道什么是执行操作的最佳方式。你能举一个例子来说明如何做到这一点吗? F.E. '[mytextfield setAction:@selector(xyz :)]' – choise 2010-12-19 13:43:54
@choise:我在代码中添加了一个目标/动作机制 – 2010-12-19 17:21:30