Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copying URL from Chrome triggers twice #16

Open
ghost opened this issue Aug 18, 2020 · 11 comments
Open

Copying URL from Chrome triggers twice #16

ghost opened this issue Aug 18, 2020 · 11 comments

Comments

@ghost
Copy link

ghost commented Aug 18, 2020

I notice that copying the URL from Chrome 84.0.4147.125 triggers the detection twice.
Seems to work as intended in every other occasion.

private void foo()
{
            clipboard = new SharpClipboard();
            clipboard.ObserveLastEntry = false;
            clipboard.ObservableFormats.Texts = true;
            clipboard.ClipboardChanged += Clipboard_ClipboardChanged;
}
        private void Clipboard_ClipboardChanged(object sender, SharpClipboard.ClipboardChangedEventArgs e)
        {
            // Get the cut/copied text.
        }
@Willy-Kimura
Copy link
Owner

Thanks. Will test this out...

@ghost
Copy link
Author

ghost commented Aug 19, 2020

Forgot to mention. The same thing happens in your NetCoreWinForms sample. Put a breakpoint on:
if (e.ContentType == WK.Libraries.SharpClipboardNS.SharpClipboard.ContentTypes.Text)
copy URL in Chrome and it should trigger twice. I'll see if I can figure something out...

Edit: Not completely sure, but other implementations struggle with the same, so I wonder if Chrome is really copying things twice. I find this strange as I thought Chrome couldn't influence the clipboard if I right click in the address bar and copy the value.... Other implementations show me that it first copies the url without "http" and then with "http"....

Anyways, one workaround is to just use a timer and ignore event that come in a close succession.

@Willy-Kimura
Copy link
Owner

Willy-Kimura commented Aug 28, 2020

That's quite a strange behaviour from Chrome. You're actually right about the copy issue. The Chrome address bar runs a script that does parse URLs on update and special events, e.g after copying/pasting.

I'll need to test this further and probably provide a custom implementation or workaround that uniquely works with chrome. For now, try using the argument e.SourceApplication.Name in the ClipboardChanged event to determine if the active application being copied from is Chrome (chrome.exe) then capture the first copy instance only and ignore the second.

@ghost
Copy link
Author

ghost commented Aug 31, 2020

Yeah, I saw the same thing happening...
I've solved with a timer as a workaround for now. I check that the content is different and reset the "cache" when the timer elapses. Works perfectly for my case. :)

        private string clipboardCache = null;
        private readonly Timer clipboardTimer = new Timer(5000);
...
        private void ClipboardTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            // Remove cache when timer elapses
            clipboardCache = null;
            clipboardTimer.Stop();
        }

// Copied value is not currently in cache
            if (e.Content.ToString() != clipboardCache)
            {
                // Update cache
                clipboardCache = e.Content.ToString();
                clipboardTimer.Start();

                // Only handle valid links
                ....
            }

@runamonk
Copy link

This same issue happens with the Windows 10 screen snip.

Click on actions/notifications.
Click screen snip, select a region.
Clipboard change is triggered twice.

Thanks

@BBUBBA
Copy link

BBUBBA commented Feb 22, 2021

It's OS Trouble

  1. you have to use register Custom ClipboardFormat and then Skip Event Custom ClipboardFormat
  2. Save Last Clipboard Info, if Same Clipboard Info Skip. (Recomend)
    • if you Binary Serialieze about Clipboard info it's Easy.

search google "clipboard event twice" or "clipboard event Echo"

if Fire Clipboard.Settext in SharpClipboard.Tests.Winform
it's Fire 2 time ChangedEvent.

also every Trouble Clear.

  1. run "notepad.exe"
  2. copy & paste in notepad ("Event")
  3. close notepad ("Event")
    ( because Clipboard Data is 2 Type)
    (1. alive with process)
    (2. not alive with process - when process die. Clipboard Data Delete)
    ( So When you close process after use clipboard. you will see Clipboard chagned event )

windows act many check clipboard point. But number 2 is Any trouble Solve.

sorry about my english not good

@DineshSolanki
Copy link

the same issue is happening on copying from firefox.

@guzelonur
Copy link

I discovered the same problem happening even on XP :)

@guzelonur
Copy link

And another problem, with this library, it detects images "twice" which were set by My.Computer.ClipBoard.SetImage method of VB.NET. There is a bug.
And also, I couldn't find a method to set image natively within this library there.

@TorstenC
Copy link

TorstenC commented Mar 15, 2022

I solved the problem with:

  • if (!IsTriggered)
  • IsTriggered = true;
  • await Task.Delay(20); Line 20 and
  • IsTriggered = false;

BTW:
In case of Chromium links I get them including the description as TextDataFormat.Html.

@SamHobbsOrg
Copy link

SamHobbsOrg commented Nov 27, 2023

I tested copying from the address bar of Edge, Chrome and Opera. With Chrome and Opera my ClipboardChanged event is called (triggered or invoked, whatever) twice. With Edge it is called 3 times.

Anyone familiar with the GetClipboardSequenceNumber function? Perhaps I misunderstand what that does but it seems to solve the problem. The WinAPI function can be used stand-alone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants