13
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NSPasteboard の変更を監視する

Last updated at Posted at 2012-08-10

ペーストボードの内容に変更があると NSPasteboard#changeCount がインクリメントされる。 KVO での監視はできないようなので素直にタイマーでポーリングをかければいい。


- (void)beginObservingPasteboard
{
    self.changeCount = [[NSPasteboard generalPasteboard] changeCount];
    [NSTimer scheduledTimerWithTimeInterval:1
                                     target:self
                                   selector:@selector(observePasteboard:)
                                   userInfo:nil
                                    repeats:YES];
}

- (void)observePasteboard:(NSTimer *)timer
{
    NSPasteboard *pboard = [NSPasteboard generalPasteboard];
    if (pboard.changeCount > self.changeCount) {
        [self pasteboardChanged:pboard];
    }
    self.changeCount = pboard.changeCount;
}

- (void)pasteboardChanged:(NSPasteboard *)pboard
{
    // Do something with pboard
}

13
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?