无法从 Mac OS X 上的后台 Java 应用程序监视系统剪贴板更改
我有一个java程序,它在后台运行并监视系统剪贴板的变化(我通过轮询来做到这一点,因为这似乎是除了“所有权变体”之外的唯一方法,我必须在其中重置所有内容是时候成为所有者了)。如果它发现特定格式的输入文本,它会处理该文本并用结果覆盖剪贴板(这样我就可以复制输入,并在程序在后台运行时粘贴结果)。
到目前为止,这在 Windows 上运行得很好,但是当在 Mac OS X 上运行相同的程序时,行为有点奇怪。只要我不将结果复制到系统剪贴板中,轮询机制本身就会按预期工作。但目前我第一次将剪贴板内容从java程序中设置出来,它仅在激活时才识别未来的外部更改。所以我不能让它在后台运行,而是我必须一直“复制输入 -> 切换到 java 程序 -> 切换回来 -> 粘贴结果”。
因为这很烦人,而且这正是我想通过“剪贴板监控 -> 结果粘贴”方法避免的事情,所以我将非常高兴了解如何解决该问题的任何想法。
编辑:一些代码片段
public void setClipboardText(String text) {
if (text == null) {
throw new NullPointerException();
}
synchronized (this.lastFoundTextLock) {
this.lastFoundText = text;
Toolkit.getDefaultToolkit().getSystemClipboard()
.setContents(new StringSelection(text), null);
}
}
public String getClipboardText() {
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().
getContents(null);
try {
if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String text = (String) t.getTransferData(DataFlavor.stringFlavor);
return text;
}
} catch (UnsupportedFlavorException e) {
} catch (IOException e) {
}
return null;
}
public void run() {
while (true) {
String currentClipboardText = getClipboardText();
boolean isNew;
synchronized (this.lastFoundTextLock) {
isNew = ((this.lastFoundText != null) || (currentClipboardText != null))
&& ((currentClipboardText == null) || !currentClipboardText
.equals(this.lastFoundText));
if (isNew) {
this.lastFoundText = currentClipboardText;
}
}
if (isNew && currentClipboardText != null) {
//new text found
fireNewClipboardTextFound(currentClipboardText);
}
try {
Thread.sleep(this.automaticCheckInterval);
} catch (InterruptedException e) {
// ignore
}
synchronized (this.monitorRunningLock) {
if (!this.monitorRunning) {
break;
}
}
}
}
I have a java program, that runs in the background and monitors the system clipboard for changes (i do this through polling, as it seems to be the only way besides the "ownership-variant", where i have to reset the content all the time to become the owner). If it discovers an input text in an specific format, it processes that text and overwrites the clipboard with the result (so i can copy the input and right after it paste the result while the program is running in background).
This worked fine so far on Windows, but when running the same program on Mac OS X, the behavior is a little bit strange. As long as i don't copy my results into the system clipboard, the polling mechanism itself works as expected. But at the moment i set the clipboard content out of the java program the first time, it recognizes future extern changes only while becoming active. So i can't just let it run in the background, but instead i have to "copy input -> switch to java-program -> switch back -> paste result" all the time.
As that is annoying and thats exactly the thing i wanted to avoid by this "clipboard monitoring -> result pasting"-method, i would be very happy for any ideas how to fix that issue.
Edit: some code-fragements
public void setClipboardText(String text) {
if (text == null) {
throw new NullPointerException();
}
synchronized (this.lastFoundTextLock) {
this.lastFoundText = text;
Toolkit.getDefaultToolkit().getSystemClipboard()
.setContents(new StringSelection(text), null);
}
}
public String getClipboardText() {
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().
getContents(null);
try {
if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String text = (String) t.getTransferData(DataFlavor.stringFlavor);
return text;
}
} catch (UnsupportedFlavorException e) {
} catch (IOException e) {
}
return null;
}
public void run() {
while (true) {
String currentClipboardText = getClipboardText();
boolean isNew;
synchronized (this.lastFoundTextLock) {
isNew = ((this.lastFoundText != null) || (currentClipboardText != null))
&& ((currentClipboardText == null) || !currentClipboardText
.equals(this.lastFoundText));
if (isNew) {
this.lastFoundText = currentClipboardText;
}
}
if (isNew && currentClipboardText != null) {
//new text found
fireNewClipboardTextFound(currentClipboardText);
}
try {
Thread.sleep(this.automaticCheckInterval);
} catch (InterruptedException e) {
// ignore
}
synchronized (this.monitorRunningLock) {
if (!this.monitorRunning) {
break;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看到其他几个人已经尝试过您正在尝试的操作( 无法从 MAC OSX 上的后台 Java 应用程序复制到剪贴板 )并且取得了一定的成功( Copying to Clipboard in Java )和一些好的答案( java/swing: 剪贴板粘贴 )但您可能想进一步调查...其他人可以对 Java 中的更改发表评论吗6 这个问题呢?
I see that several others have attempted what you're trying ( Can't copy to a clipboard from a background java application on MAC OSX ) and had marginal success ( Copying to Clipboard in Java ) and few good answers ( java/swing: clipboard paste ) but you might want to investigate further... Can anyone else comment on the changes in Java 6 wrt this issue?