Question

I'm using the following code to detect the return key in a text view. How do you detect if the shift key is being pressed too?

- (BOOL)textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
{
    if(commandSelector == @selector(insertNewline:))
    {
        // return key
        return YES;
    }

    return NO;
}
Was it helpful?

Solution

subclass NSTextView and override flagsChanged with this

-(void) flagsChanged:(NSEvent *)theEvent {

    if ([theEvent modifierFlags] & NSShiftKeyMask && [theEvent modifierFlags] & NSCommandKeyMask) {

       NSLog(@"pressed");
    }
}

OTHER TIPS

Swift 4

override func flagsChanged(with event: NSEvent) {

    if event.modifierFlags.contains(.shift) {
        print("shift!")
    }

    super.flagsChanged(with: event)
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top