Clipboard - New content notifications?

Is there a (recommended?) way to know when the clipboard content changes?

Any help/suggestions will be appreciated.

Ken.

To be notified you’ll need to be able to receive raw window messages from the system and then register to receive clipboard notification messages. This is doable in Xojo but not simple; see the HotKeyHelper class in the Windows Functionality Suite for an example of receiving window messages.

A much simpler solution is to poll the clipboard sequence number:

Function HasClipboardChanged() As Boolean
  Declare Function GetClipboardSequenceNumber Lib "User32" () As Integer
  Static lastSeqNum As Integer
  Dim current As Integer = GetClipboardSequenceNumber()
  If current <> lastSeqNum Then
    lastSeqNum = current
    Return True
  End If
End Function

Thanks Andrew.

So would I put that code onto a timer to constantly monitor the clipboard?

Yes. I’d suggest using the shortest possible period (1ms) for the Timer.

Many thanks. :wink:

You cant use a period shorter than 16ms in windows with regular timers.

The propper way is with declares: sensing when the clip board has changed - #10 by David_Cox

But the timer can work Ok

The intent of using a 1ms period here is to have the timer run on every iteration of the event loop, however frequently that may be.

1 Like

Then you can use “timer timeout of 0 that means the next event loop” you can use timer.Calllater 0, AddressOf AMethod

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.