Cocoa 检测移位+;在NSTextView中返回
我使用以下代码在文本视图中检测返回键。如何检测换档键是否也被按下Cocoa 检测移位+;在NSTextView中返回,cocoa,nstextview,Cocoa,Nstextview,我使用以下代码在文本视图中检测返回键。如何检测换档键是否也被按下 - (BOOL)textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector { if(commandSelector == @selector(insertNewline:)) { // return key return YES; } return NO; } 子类NSText
- (BOOL)textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
{
if(commandSelector == @selector(insertNewline:))
{
// return key
return YES;
}
return NO;
}
子类NSTextView和override flagsChanged
-(void) flagsChanged:(NSEvent *)theEvent {
if ([theEvent modifierFlags] & NSShiftKeyMask && [theEvent modifierFlags] & NSCommandKeyMask) {
NSLog(@"pressed");
}
}
Swift 4
override func flagsChanged(with event: NSEvent) {
if event.modifierFlags.contains(.shift) {
print("shift!")
}
super.flagsChanged(with: event)
}
成功了!对于shift键,我只需要if([theEvent modifierFlags]&NSShiftKeyMask)。