在Mac OS X剪贴板监视| Java的 [英] Clipboard monitoring on Mac OS X | Java

查看:1085
本文介绍了在Mac OS X剪贴板监视| Java的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦,从Mac OS X上的系统剪贴板中获取数据
我想要做的就是听系统剪贴板,每一个新的[文字为基础]的信息放进去的时间内打印剪贴板中的内容。

I'm having troubles getting data from the system clipboard on Mac OS X. What I'm trying to do is to listen to the system clipboard and print the content of the clipboard each time new [text based] information is put into it.

问题:波纹管code适用于Windows 7和openSUSE Linux机器完全正常,但是当我尝试运行Mac OS X上同一code中的程序无法打印剪贴板中,直到焦点的新内容被提供给应用程序。 [什么也没有打印,直到我上的应用程序图标,点击被告席...]

The problem: bellow code works perfectly fine on Windows 7 and openSUSE Linux machines, however when I try running the same code on Mac OS X the program fails to print the new content of the clipboard until focus is given to the application. [Nothing is printed until I click on the application icon on the dock...]

我的源$ C ​​$ C:

My source 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);  
    }  

    public void gainOwnership(Transferable t){ 
        try {this.sleep(100);} 
        catch (InterruptedException e) {e.printStackTrace();}
        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();
        while(true){}}

}

我缺少什么/做错了吗?

What I'm missing/doing wrong?

[更新]
我发现这里张贴类似的问题:<一href=\"http://stackoverflow.com/questions/10039275/java-thread-goes-to-sleep-when-not-in-focus-on-osx\">Java线程去的时候不睡在重点OSX
但是使用命令Java的罐子myApp.jar&安培;没有作为一种变通方法为我工作。

[Update] I found similar problem posted here: Java thread goes to sleep when not in focus on OSX However using the command "java -jar myApp.jar &" didn't work as a workaround for me.

推荐答案

这似乎是一个开放的bug,看的如果应用不集中MACOSX_PORT-511 ClipboardOwner方法lostClipboard不叫。

This seems to be an open bug, see MACOSX_PORT-511 ClipboardOwner method lostClipboard is not called if app is not focused.

这篇关于在Mac OS X剪贴板监视| Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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