0

要点を言えば、次のようになります。

最初のスニペット(AppDelegate):

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    //...code taken out...

    [NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *incomingEvent) {
        if ([incomingEvent type] == NSKeyDown) {
            NSUInteger flags = [incomingEvent modifierFlags] & NSDeviceIndependentModifierFlagsMask;
            if (flags==NSCommandKeyMask && ([incomingEvent keyCode] == 8)) {
                [ClipboardUtilities logger:@"cmd+c recognized"];
                [self determineAndAddToHistory];
            }
        }
    }];
}

2番目のスニペット(AppDelegate):

-(void) determineAndAddToHistory {
    id clipDat = [ClipboardUtilities getClipboardDataNatively];
    if ([clipDat isKindOfClass:[NSAttributedString class]])
        NSLog(@"clipDat.string = %@",((NSAttributedString*)clipDat).string);
}

3番目のスニペット(ClipboardUtilitiesクラス)​​:

+(id) getClipboardDataNatively {
    NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
    NSArray *classArray = @[[NSAttributedString class], [NSImage class]];
    NSDictionary *options = [NSDictionary dictionary];

    NSArray *objectsToPaste = nil;
    BOOL ok = [pasteboard canReadObjectForClasses:classArray options:options];
    if (ok) {
        objectsToPaste = [pasteboard readObjectsForClasses:classArray options:options];
    }
        NSLog(@"objectsToPaste count = %li",[objectsToPaste count]);
    return [objectsToPaste objectAtIndex:0];
}

例を挙げて説明しようとする奇妙な動作に気づきました。

入力

  1. Cmd+C文字列「A」
  2. Cmd+C文字列「B」
  3. Cmd+C文字列「C」
  4. Cmd+C文字列「D」

defineAndAddToHistoryからの出力

  1. A
  2. A
  3. B
  4. C

そのため、何らかの理由で最初のアイテムが保持されていることに気づきました...その後、毎回2番目に新しいアイテムが返されます。getClipboardDataNativelyメソッドでobjectsToPaste配列を出力しようとしましたが、これはまだ当てはまります。誰かが私にこの問題にどのように取り組むか、または彼らがそれをどのように解決したかを教えてもらえますか?

PS my ClipboardUtilitiesクラスは、デリゲートを実装していないか、NSObject以外から継承していません。

4

1 に答える 1

0

長い質問が好きな人はいないので (これを短くする方法を見つけなければなりません)、私は何かを考え出しました。何らかの理由で、ホットキーの呼び出しが非常に迅速に行われます (キーが実際に呼び出された後にクリップボードが更新されます)。その結果、わずかな遅延が発生しただけで、モデルが適切に更新されるようになりました。

NSTimer* briefDelay = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(determineAndAddToHistory) userInfo:nil repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:briefDelay forMode:NSRunLoopCommonModes];

ドキュメントに従って、タイマーを手動で無効にしません。

repeats
    If YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.
于 2012-11-14T04:31:06.540 に答える