NSTextField支持Cmd+C/V快捷键复制、粘贴

 

NSTextField+Copypast.h

1 #import <Cocoa/Cocoa.h>
2 
3 @interface NSTextField (Copypast)
4 
5 - (BOOL)performKeyEquivalent:(NSEvent *)event;
6 
7 @end

 

NSTextField+Copypast.m

 1 #import "NSTextField+Copypast.h"
 2 
 3 @implementation NSTextField (Copypast)
 4 
 5 - (BOOL)performKeyEquivalent:(NSEvent *)event {
 6     if (([event modifierFlags] & NSDeviceIndependentModifierFlagsMask) == NSCommandKeyMask) {
 7         // The command key is the ONLY modifier key being pressed.
 8         if ([[event charactersIgnoringModifiers] isEqualToString:@"x"]) {
 9             return [NSApp sendAction:@selector(cut:) to:[[self window] firstResponder] from:self];
10         } else if ([[event charactersIgnoringModifiers] isEqualToString:@"c"]) {
11             return [NSApp sendAction:@selector(copy:) to:[[self window] firstResponder] from:self];
12         } else if ([[event charactersIgnoringModifiers] isEqualToString:@"v"]) {
13             return [NSApp sendAction:@selector(paste:) to:[[self window] firstResponder] from:self];
14         } else if ([[event charactersIgnoringModifiers] isEqualToString:@"a"]) {
15             return [NSApp sendAction:@selector(selectAll:) to:[[self window] firstResponder] from:self];
16         }
17     }
18     return [super performKeyEquivalent:event];
19 }
20 
21 @end

 

posted @ 2017-07-25 10:02  Kaymin  阅读(906)  评论(0编辑  收藏  举报