当将东西添加到NSPasteboard时收到通知
在我的应用程序中,我想收到通知如果有东西添加到NSPasteboard。如果我从任何其他程序复制文本,我希望我的应用程序知道它。
In my application I want to get notified if something is added into the NSPasteboard. If I copy an text from any other program I want my application to know about it.
在某个地方我读不能这样做。我应该创建一个计时器,并自己检查NSPasteboard的内容。
Somewhere I read it can't be done that way. I should create a timer and check the content of the NSPasteboard myself.
这是要做的方法吗?
是的,您必须轮询粘贴板以查看其内容是否已更改。这不是理想,但它是可能的。基本上,您有一个计时器,每秒触发一次或两次,并检查 - [NSPasteboard changeCount]
。如果 changeCount
更改,这意味着粘贴板的内容也已更改(或至少有一个新的所有者)。
Yes, you essentially have to poll the pasteboard to see if its contents have changed. It's not ideal, but it's possible. Basically, you have a timer that fires once or twice a second and checks the -[NSPasteboard changeCount]
. If the changeCount
changes, that means the contents of the pasteboard have also changed (or there's at least a new owner).