无法从Mac OS X上的后台Java应用程序监视系统剪贴板更改 [英] Can't monitor system clipboard changes from a background java application on Mac OS X

查看:138
本文介绍了无法从Mac OS X上的后台Java应用程序监视系统剪贴板更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java程序,该程序在后台运行并监视系统剪贴板中的更改(我通过轮询来执行此操作,因为这似乎是所有权变量"之外的唯一方法,在该方法中,我必须重置内容始终成为所有者).如果它发现特定格式的输入文本,则将处理该文本并用结果覆盖剪贴板(以便我可以复制输入,并在程序在后台运行时将结果粘贴后立即粘贴).

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).

到目前为止,该方法在Windows上运行良好,但是在Mac OS X上运行相同的程序时,其行为有点奇怪.只要我不将结果复制到系统剪贴板中,轮询机制本身就会按预期工作.但是目前我第一次将剪贴板内容设置为Java程序之外,它仅在激活时才识别将来的外部更改.因此,我不能只让它在后台运行,而是必须一直复制输入->切换到Java程序->切换回->粘贴结果".

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.

一些代码问题

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;
      }
    }
  }
}

推荐答案

我看到其他几个人尝试了您要尝试的东西(复制到Java中的剪贴板)和几个好的答案(java/swing:剪贴板粘贴),但您可能想进一步研究...还有人可以评论Java 6 wrt中的更改吗?这个问题?

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?

这篇关于无法从Mac OS X上的后台Java应用程序监视系统剪贴板更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆