1

"How can I get an event to see what is copied to the clipboard?".... is a very common and old questions. Just like for example this forum question

My experience is that some applications fire the event multiple times. I have found people say to only deal with the last time it is fired. How exactly should I do this? How do i know the last time?

Further when I give my Outlook 2007 focus it fires the clipboard and fires it multiple times. Also when I click around in Outlook the clipboard message is fired. How am I supposed to differentiate between a real copy of something to the clipboard and these other messages which I don't care about.

I am aware I can check if the contents is the same, but in my case this is a limitation as the use might copy the same contents twice.

I am aware of this unanswered question here. This is only half of my questions and people head off on the copying of different types in any case.

Community
  • 1
  • 1
darbid
  • 2,545
  • 23
  • 55
  • Clipboard viewers are brittle, they have to co-operate with all other viewers. Like the one that Office programs use. If you want to know if the clipboard content changed then just don't use a viewer. Call AddClipboardFormatListener() and pay attention to the WM_CLIPBOARDUPDATE you'll now get. – Hans Passant Jun 17 '15 at 13:38
  • I changed to WM_CLIPBOARDUPDATE. Lets get specific. When I click on Outlook - WM_CLIPBOARDUPDATE is fired 3 times - the text is exactly the same and the Formats of the data in the clipboard are also exactly the same. So what "updated" or "changed"? – darbid Jun 18 '15 at 09:00
  • You'll have to ask Microsoft. Why does this matter? It shouldn't. – Hans Passant Jun 18 '15 at 09:03
  • 1. User copies something. 2. my app deals with what they have copied. 3. 2 hours later user clicks on Outlook 4. The same thing appears (lets forget 3 times) but the same thing appears in the clipboard. How do I know if the user clicked on Outlook or in fact copied text to the clipboard and my app needs to act on it? – darbid Jun 18 '15 at 09:09
  • You don't know of course, the clipboard says nothing about where the data came from. You could hack GetForegroundWindow() perhaps. [Read this](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Hans Passant Jun 18 '15 at 09:19
  • Thank you. You are confirming what I thought - this is a relief. By the way knowing the foregroundwindow is Outlook will not help. In any case in the absence of a better conclusion I am going to conclude, I only accept the same text once. If the text in the clipboard does not change I don't act on it. This is a little sad but the event that the user copies the same text twice is not going to happen too often. – darbid Jun 18 '15 at 10:41

1 Answers1

0

Thank you Hans for giving me some things to think about. I don't have code as an answer but can give some hints on how I solved this problem for me.

  1. Outlook fires the clipboard multiple times and even fires the clipboard when it gets focus. Solution - I only react on one of the events within 200ms. I also only react on a clipboard event when the control key is pressed. (this is a special case for me as I only want to do this for keyboard copy and not mouse copies)
  2. When the event is fired it is being fired from another program so you cannot access the clipboard yet. Solution - Only one program can access it, so you need to keep trying after the event has happened, you also need to hope that the other program releases the clipboard so you can look at it.
darbid
  • 2,545
  • 23
  • 55