Willy-Kimura / SharpClipboard

A library for anonymously monitoring clipboard entries.
188 stars 36 forks source link

Clipboard Image detected for twice #26

Open guzelonur opened 2 years ago

guzelonur commented 2 years ago

Hi,

I think there might be some bug with the library itself, or the problem might be underlying on System.Windows.Forms.Clipboard class, when I set an image using System.Windows.Forms.Clipboard.SetImage() method, SharpClipboard's ClipboardChanged event is fired TWICE for no reason.

Is there anyone noticed that problem which occurs when you copy/set any image using Clipboard.SetImage() method?

Best regards.

Mehrdad32 commented 2 years ago

Hi everyone, I handled this issue with set and check last object that I get from clipboard and if they were equal I ignore second object!

MontanaCode commented 2 years ago

The Clipboard.SetImage() method changes the clipboard twice: 1 - It clears the clipboard 2 - It loads the new image into the clipboard.

This is also true of Clipboard.SetText() and others ...

The way I handle this is to create a global class property (integer) called "Ignore". Every time I use the various .Set methods, I first increment (increase) the "Ignore" property by 2. When the WM message comes in (or when SharpClipboard's "ClipboardChanged" event is fired), I have an IF statement, so that it will only process that clipboard message if "ignore" = 0. Each time we ignore a message, we decrement (decrease) the "Ignore" property by 1 after we ignore the message. This will allow us to know when an end-user has copied something new to the clipboard, vs. when we have changed the clipboard ourselves with our code.

Best of luck to all in their coding.