我们如何获得有关系统剪贴板事件的通知? [英] How do we get notified about system clipboard events?

查看:31
本文介绍了我们如何获得有关系统剪贴板事件的通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我想编写一个简单的程序(仅用于学习目的)来监视系统剪贴板数据并序列化其内容.

Hi all I want to write a simple program (just for learning purposes) that monitors the system clipboard data and serializes its contents.

例如,每当用户将一些数据复制到系统剪贴板(Ctrl-C )时,我的程序应该收到通知"并序列化剪贴板数据成一个文件.

For example, whenever the user copies some data into the system clipboard (Ctrl-C etc), my program should receive a "notification" and serialize the clipboard data into a file.

我研究了 java.awt.datatransfer 但似乎没有任何方法可以在将数据复制到剪贴板时将回调挂接到系统事件上.

I've looked into java.awt.datatransfer but there doesn't seem to be any way I could hook a callback onto the system event whenever data is copied to the clipboard.

我们如何获得有关系统剪贴板事件的通知?

How do we get notified about system clipboard events?

如果该解决方案仅适用于 Windows 也可以,但操作系统互操作性当然是一个加分项.

推荐答案

尝试附加一个 FlavorListenerClipboard 通过调用 Clipboard.addFlavorListener.

Try attaching a FlavorListener to the Clipboard by calling Clipboard.addFlavorListener.

请参阅此 SO 问题,其中包含代码示例和已接受的答案:是否可以在java之外的剪贴板内容发生变化时得到通知

See this SO question which has a code sample and accepted answer: Is it possible to be informed when clipboard content changes outside of java

更新:

那没有用 - 正如 camickr 指出的那样,只有在 DataFlavor 改变时才会触发.您似乎有两个选择:

That didn't work - as camickr pointed out, that only fires when the DataFlavor changes. It appears there are two options for you:

  1. 收听 WindowEvents 并在您的应用获得焦点时检查剪贴板的内容.如果您不要求您的应用在后台对剪贴板内容执行任何操作,这将是合适的,换句话说,用户必须使用剪贴板上的内容切换到您的应用.

  1. Listen to WindowEvents and when your app gets focus check the contents of the clipboard. This would be suitable if you don't require your app to do something with the clipboard contents in the background, in other words, the user must switch to your app with something on the clipboard.

关注这个示例 并定期轮询剪贴板.显然你会忽略 Mac 特定的东西(以及糟糕的单例实现),但想法是一样的.

Follow this example and poll the clipboard periodically. Obviously you would ignore the Mac-specific stuff (and the poor singleton implementation) but the idea is the same.

这篇关于我们如何获得有关系统剪贴板事件的通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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