Pause required before WinClip.GetFormats()

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
JnLlnd
Posts: 490
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Pause required before WinClip.GetFormats()

06 Feb 2024, 11:36

With the WinClip library and AHK v1.1, I'm using the method WinClip.GetFormats() to inspect the formats currently in the Clipboard. It returns an array of formats with name and size for each format.

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
}
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CoffeeChaton and 117 guests