Used after a Clipboard change (using the OnClipboardChange() function), it works well but only if a small pause is inserted between the clipboard change and the call to the GetFormats() method. Usually, a 200 ms pause is sufficient. But in some cases, especially using MS Office apps (like WinWord), the result is incomplete after only 200 ms. I've found that I need to wait up to 500 ms to be sure(?) that the array is complete.
My question: is there a way to determine that the Clipboard is ready to be inspected before calling the GetFormats() method? I'd perfer avoid pausing for a fixed and long amount of time each time the clipboard is changed and wait only the time necessary to assure the data is ready. I've tried this WaitForClipboard() function in the example below but is does not seem to do the job.
Sample code:
Code: Select all
#SingleInstance Force
#requires AutoHotkey v1.1
#InstallKeybdHook
#Include %A_ScriptDir%\Lib\WinClipAPI.ahk ; include this first (https://www.autohotkey.com/boards/viewtopic.php?t=29314)
#Include %A_ScriptDir%\Lib\WinClip.ahk
OnClipboardChange("ClipboardContentChangedWait")
return
ClipboardContentChangedWait(intClipboardContentType)
; called by OnClipboardChange; intClipboardContentType: 0 = empty / 1 = contains text / 2 = contains binary
{
if !WaitForClipboard() ; used instead of sleep, 200
{
MsgBox, Clipboard busy
return
}
o_Formats := WinClip.GetFormats()
strFormats := intClipboardContentType
for intFormat, oItem in o_Formats
strFormats .= " | " . oItem.Name
MsgBox, %strFormats%
}
WaitForClipboard()
{
while, pClipboardOwner := DllCall("GetOpenClipboardWindow")
if (A_Index > 10)
return false ; Clipboard is locked by another application for more than one second
else
Sleep, 100
return true
}