使用JavaFx在系统剪贴板上收听 [英] Listening on system clipboard using JavaFx

查看:227
本文介绍了使用JavaFx在系统剪贴板上收听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如对这个问题的回答中所述,可以设置时间轴来检查系统剪贴板中是否有更改:

As stated in the answer to this question, one can setup a Timeline to check whether there is a change in the system clipboard:

在javafx 8的时间轴外部设置和使用变量

但是有更好的方法吗?例如,事件监听器?我搜索了JavaFx 8文档,没有发现任何明显有用的东西。

But is there a better way? For example, an event listener? I have searched JavaFx 8 doc and didn't find anything obviously helpful.

首选使用JavaFx的解决方案,但欢迎所有答案。

Solutions using JavaFx is preferred, but all answers are welcome.

推荐答案

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.Clipboard;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        final Clipboard systemClipboard = Clipboard.getSystemClipboard();

        new com.sun.glass.ui.ClipboardAssistance(com.sun.glass.ui.Clipboard.SYSTEM) {
            @Override
            public void contentChanged() {
                System.out.print("System clipboard content changed: ");
                if ( systemClipboard.hasImage() ) {
                    System.out.println("image");
                } else if ( systemClipboard.hasString() ) {
                    System.out.println("string");
                } else if ( systemClipboard.hasFiles() ) {
                    System.out.println("files");
                }
            }
        };

        primaryStage.setScene(new Scene(new StackPane()));
        primaryStage.show();
    }

}

测试:


  • 按打印屏幕键

  • 按Ctrl + C键选择字符串

  • Ctrl + c表示选定的文件

这篇关于使用JavaFx在系统剪贴板上收听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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