Can I receive a callback whenever an NSPasteboard is written to?

后端 未结 6 1904
迷失自我
迷失自我 2020-12-02 09:26

I\'ve read Apple\'s Pasteboard Programming Guide, but it doesn\'t answer a particular question I have.

I\'m trying to write a Cocoa application (for OS X, not iOS) t

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-02 09:45

    It's not necessary to poll. Pasteboard would generally only be changed by the current view is inactive or does not have focus. Pasteboard has a counter that is incremented when contents change. When window regains focus (windowDidBecomeKey), check if changeCount has changed then process accordingly.

    This does not capture every change, but lets your application respond if the Pasteboard is different when it becomes active.

    In Swift...

    var pasteboardChangeCount = NSPasteboard.general().changeCount
    func windowDidBecomeKey(_ notification: Notification)
    {   Swift.print("windowDidBecomeKey")
        if  pasteboardChangeCount != NSPasteboard.general().changeCount
        {   viewController.checkPasteboard()
            pasteboardChangeCount  = NSPasteboard.general().changeCount
        }
    }
    

提交回复
热议问题