1

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

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.

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?

It's ok if the solution works only for windows, but OS inter-operability is a plus of course.

Pacerier
  • 86,231
  • 106
  • 366
  • 634
  • 1
    *"I want to write a simple program (just for learning purposes) that monitors the system clipboard data."* Any program that intends to intercept data not specifically meant for itself, is inherently suspect. For that reason I hope this app. is 'not simple'. – Andrew Thompson Dec 29 '11 at 06:41

1 Answers1

3

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

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

Update:

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

  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.

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

Community
  • 1
  • 1
Paul
  • 19,704
  • 14
  • 78
  • 96
  • 1
    This only listens for a flavour change, not when data is copied to the clipboard. So for example if 3 pieces of text are copied in a row you will not be notified of the last 2 and potentially the first as well depending on what data was originally in the clipboard. – camickr Dec 29 '11 at 07:00
  • That's what I thought too but the discussion underneath the code indicates otherwise. I just ran that code...no luck. Am taking a closer look now. – Paul Dec 29 '11 at 07:21
  • @camickr not even that.. it somehow doesn't work with my test. I have 2 different characters "A" and "B" in a blank notepad. I highlight "A" and Ctrl-C. Then I highlight "B" and Ctrl-C. And I repeat this cycle for a couple more times. Somehow the program gets notified for the first copy operation and ignores the rest. – Pacerier Dec 29 '11 at 08:51
  • @Pacerier that's how it's supposed to work - the event fires when the flavor of the data changes, not the content. The method that fires the event (in `Clipboard`) could fire every time something shows up on the clipboard but it doesn't - it filters out events where the flavor is unchanged. – Paul Dec 29 '11 at 14:36
  • @Paul yes are you aware if there is a "content listener" as well for that purpose? – Pacerier Dec 29 '11 at 15:13
  • There is no code at the example link. Can someone copy it here please? – Yster Dec 01 '15 at 08:55