在Mac OS中监控剪贴板 [英] Monitor clipboard in Mac OS
问题描述
我需要在我的mac os应用程序监视剪贴板事件。我找到了剪贴板查看器和另一个问题在stackoverflow要求同样的事情,但没有人有一个解决方案如何监视剪贴板事件。
I need to monitor clipboard events in my mac os app. I found a sample for a clipboard viewer and another question in stackoverflow asking for the same thing, but none of them has a solution on how to monitor the clipboard events.
也就是说,在用户点击命令+ c后,我立即得到一个事件通知。我知道该功能存在,因为有一个应用使用此功能功能
That is, immediately after the user hits command + c, I get an event notifying. I know that the functionality exists, as there is an app that uses this functionality
想法?
推荐答案
它会打印输入到剪贴板的每一个基于文本的信息]看到下面的代码:
I have written a clipboard listener [it will print every new text based information that entered the clipboard] in native Java see the following code:
import java.awt.Toolkit;
import java.awt.datatransfer.*;
import java.io.IOException;
public class ClipboardListener extends Thread implements ClipboardOwner {
Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
public void run(){
Transferable selection = systemClipboard.getContents(this);
gainOwnership(selection);
while (true) {}
}
public void gainOwnership(Transferable t){
try {this.sleep(100);}
catch (InterruptedException e) {}
systemClipboard.setContents(t, this);
}
public void lostOwnership(Clipboard clipboard, Transferable contents) {
try {System.out.println((String) clipboard.getData(DataFlavor.stringFlavor));}
catch (UnsupportedFlavorException e) {}
catch (IOException e) {}
gainOwnership(contents);
}
}
public class myApp {
public static void main(String[] args){
ClipboardListener listener = new ClipboardListener();
listener.start();}
}
应用程序将需要焦点从剪贴板获取事件。 [我不是Mac OS X开发人员,所以我不怎么解决这个问题,实际上我发布了一个 question about it ...]
It works, but the application will need focus to get the event from the clipboard. [I'm not Mac OS X developer so I don't how to fix this, actually I have posted a question about it...]
这篇关于在Mac OS中监控剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!