0

I use CMD-C for my custom COPY action which consumes the COPY shortcut also for the usual text field editing. I want to preserve the standard CMD-C copy to clipboard when a text field has focus and is editing. How do I do that?

PerfectGamesOnline.com
  • 1,774
  • 1
  • 18
  • 31
  • 1
    You're supposed to implement a custom copy action by implementing a `copy:` method in the responder chain (for example, on a custom view or in your view controller or in your window controller or in your app delegate). Why aren't you doing that? – rob mayoff Nov 30 '18 at 15:06
  • Good point, thanks. – PerfectGamesOnline.com Nov 30 '18 at 15:52

1 Answers1

0

I've managed to intercept the menu shortcut events but I'm struggling with proper routing the onKey event to current firstResponder textField.

The code bellow works but I'm not totally happy with it. See the comments there. This method overwrites the original one in NSApplication.

- (void) sendEvent:(NSEvent*)event
{
    id fr = NSApplication.sharedApplication.keyWindow.firstResponder;
    if (fr == nil || [fr isKindOfClass:NSTextView.class] == NO) {
        [super sendEvent:event];
        return;
    }
    NSTextView *tf = (NSTextView*)fr;
    if (!tf.editable || !tf.isEditable) {
        [super sendEvent:event];
        return;
    }

    // <REF> https://stackoverflow.com/a/3176930/929170
    //
    if ([event type] == NSEventTypeKeyDown) {
        if (([event modifierFlags] & NSEventModifierFlagDeviceIndependentFlagsMask) == NSEventModifierFlagCommand) {
            if ([[event charactersIgnoringModifiers] isEqualToString:@"x"]) {
                if ([self sendAction:@selector(cut:) to:nil from:self])
                    return;
            }
            else if ([[event charactersIgnoringModifiers] isEqualToString:@"c"]) {
                if ([self sendAction:@selector(copy:) to:nil from:self])
                    return;
            }
            else if ([[event charactersIgnoringModifiers] isEqualToString:@"v"]) {
                if ([self sendAction:@selector(paste:) to:nil from:self])
                    return;
            }
            else if ([[event charactersIgnoringModifiers] isEqualToString:@"z"]) {
                if ([self sendAction:@selector(undo:) to:nil from:self])
                    return;
            }
            else if ([[event charactersIgnoringModifiers] isEqualToString:@"a"]) {
                if ([self sendAction:@selector(selectAll:) to:nil from:self])
                    return;
            }
        }
        else if (([event modifierFlags] & NSEventModifierFlagDeviceIndependentFlagsMask) == (NSEventModifierFlagCommand | NSEventModifierFlagShift)) {
            if ([[event charactersIgnoringModifiers] isEqualToString:@"Z"]) {
                if ([self sendAction:@selector(redo:) to:nil from:self])
                    return;
            }
        }
    }

    // <C> this likely takes care of routing all the standard events (e.g. select word) EXCEPT clipboard actions;
    // Don't know why the clipboard events are not handled but the code above takes care of it.
    // Some event might however be lost...
    //
    [NSApplication.sharedApplication.keyWindow sendEvent:event];

    // <C> super is already handled above;
    //[super sendEvent:event];
}
PerfectGamesOnline.com
  • 1,774
  • 1
  • 18
  • 31