Clipboard: 'PasteWait': wait for paste to complete

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Clipboard: 'PasteWait': wait for paste to complete

19 Sep 2017, 03:54

In AutoHotkey, we have the very useful ClipWait command, which waits for a cut/copy operation to complete.

I've written this, which waits for a paste operation to complete, it works in Notepad, but it doesn't work in Excel. I was wondering if people had any other ideas.

Code: Select all

q:: ;test PasteWait
vOutput := ""
VarSetCapacity(vOutput, 55*1000000*2)
;we need to make the number big enough to cause a delay,
;but preferably small enough that it doesn't take an unnecessarily long time:
if WinActive("ahk_class XLMAIN")
	vNum := 1000000
else
	vNum := 200000
Loop, % vNum
	vOutput .= "abcdefghijklmnopqrstuvwxyz`tabcdefghijklmnopqrstuvwxyz`r`n"
Clipboard := vOutput
SendInput, ^v
Sleep, 1000

;wait for a paste operation to complete
Loop
{
	if DllCall("user32\OpenClipboard", "Ptr",0)
	{
		DllCall("user32\CloseClipboard")
		break
	}
	Sleep, 50
}
SoundBeep
return
[EDIT:] See also:
[WM_CUT/WM_COPY/WM_PASTE][WM_CLEAR/WM_UNDO]
"Clipboard :=" does not always empty the clipboard immediately - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=63356&p=271517#p271517
Last edited by jeeswg on 07 Apr 2019, 20:46, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Clipboard: 'PasteWait': wait for paste to complete

19 Sep 2017, 15:42

Here's a variant, using GetOpenClipboardWindow instead of OpenClipboard.

It appears that in Excel, that there is a delay between the clipboard being made available again and Excel being completely ready to use again. As soon as the SoundBeep occurs, you can start copying and pasting again, and the paste operation will complete.

In Excel, you can check the mouse cursor, to confirm that the paste operation has completely finished.

So, we have Notepad and Excel sussed so far. (Tested with Excel 2007.)

Code: Select all

q:: ;'PasteWait'
vOutput := ""
VarSetCapacity(vOutput, 55*1000000*2)
if WinActive("ahk_class XLMAIN")
	vNum := 1000000
else
	vNum := 200000
Loop, % vNum
	vOutput .= "abcdefghijklmnopqrstuvwxyz`tabcdefghijklmnopqrstuvwxyz`r`n"
Clipboard := vOutput
SendInput, ^v
Sleep, 1000
;wait for a paste operation to complete
while DllCall("GetOpenClipboardWindow", Ptr)
	Sleep 50
if WinActive("ahk_class XLMAIN")
	while (A_Cursor = "Wait")
		Sleep 50
SoundBeep
return
Links:
Verifying a successful pasting event - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=33797
Robust copy and paste routine (function) - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/1118 ... -function/
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Clipboard: 'PasteWait': wait for paste to complete

19 Sep 2017, 18:20

Good topic jeeswg :thumbup:
Do the cursor not change to something else if the cursor is outside of the window?
In function, based on your code,

Code: Select all

pasteWait(timeout:=50){
	; v2.
	; returns 1 if wait completed withing the timeout time. 0 if timed out
	static res:=10
	local r, tic:=a_tickcount
	while (r:=DllCall("GetOpenClipboardWindow", "Ptr")) && a_tickcount - tic < timeout
		sleep(res)
	return !r
}
User avatar
SL5
Posts: 879
Joined: 12 May 2015, 02:10
Contact:

Re: Clipboard: 'PasteWait': wait for paste to complete

12 Dec 2018, 18:26

i using for waiting for clipboard this this method. but i always interested for better solution.
dont know if it help with your problem.

Code: Select all

				if(true){
				;/¯¯¯¯ SendLevel9 ¯¯ 181212214527 ¯¯ 12.12.2018 21:45:27 ¯¯\
				suspend,on
					ClipboardBackup := Clipboard
					SendLevel 9 ; with this additions lines it works also in globalIntelisense nearliy 99% of time 18-04-01_12-24
					Clipboard := ""
					Clipboard := sending ; " ln=" A_LineNumber "`n`n"
				    ;AHKcode := "Critical, On`n"
					AHKcode := "Send,^v"
					DynaRun(AHKcode) ; <= uese old clipboard.or  simle give it more time

					; wait sending stated
					while(A_TimeIdleKeyboard > 50) ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=59913&p=252749#p252749
						sleep,50

					; check is sending finised
					while(A_TimeIdleKeyboard < 50) ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=59913&p=252749#p252749
						sleep,50

					Clipboard := ClipboardBackup
					sleep,100
					SendLevel 0
					suspend,off
            ;\____ SendLevel9 __ 181212214540 __ 12.12.2018 21:45:40 __/
				}
freespacing
Posts: 150
Joined: 28 Sep 2016, 11:14
Contact:

Re: Clipboard: 'PasteWait': wait for paste to complete

05 Apr 2019, 10:50

Hi @jeeswg, hi @Helgef, hi @SL5

Hope you've all had a great week.

Writing on this thread because I'm desperate for a PasteWait() function that works so that I can guarantee that when I refill the clipboard, pasting has already finished. Right now I have to do this with a Sleep, 500:

Code: Select all

tmp_clip := ClipboardAll ; preserve old clipboard
Clipboard := text
ClipWait ; wait until clipboard contains data
Send, ^v
Sleep, 500  ; prevents the clipboard from changing during the paste operation
                ; in which case it pastes tmp_clip which is being assigned below!
Clipboard := tmp_clip ; restore the clipboard
ClipWait
The PasteWait() functionality you are describing sounds perfect for this need, but your code is way over my head and I haven't managed it to turn it into a simple function that can replace my Send, ^v or my Sleep, 500

Would you be willing to explain how to use your functions in this context?
That would be tremendously helpful, not just for me but others with the same challenge.

In advance, huge thanks.
:thumbup:
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Clipboard: 'PasteWait': wait for paste to complete

05 Apr 2019, 10:55

It's basically just this. While a window has control of the clipboard, keep waiting.

Code: Select all

while DllCall("user32\GetOpenClipboardWindow", "Ptr")
	Sleep, 50
And this variant. Try to open the clipboard for writing, if you succeed, close it, else, keep waiting.

Code: Select all

Loop
{
	if DllCall("user32\OpenClipboard", "Ptr",0)
	{
		DllCall("user32\CloseClipboard")
		break
	}
	Sleep, 50
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
freespacing
Posts: 150
Joined: 28 Sep 2016, 11:14
Contact:

Re: Clipboard: 'PasteWait': wait for paste to complete

05 Apr 2019, 11:12

@jeeswg Thank you for cutting it down to the essentials for the simpler-minded. :)

Just to be sure I understand, because at the moment it's pasting the wrong thing.
Are you saying:

Code: Select all

; ------------- Critical function for PasteThis()
PasteWait(){
    ; https://www.autohotkey.com/boards/viewtopic.php?f=5&t=37209&p=171360#p271287
    while DllCall("user32\GetOpenClipboardWindow", "Ptr")
    	Sleep, 50
}

PasteThis(text) {
   tmp_clip := ClipboardAll ; preserve old clipboard 
   Clipboard := "" ; empty the clipboard so the ClipWait can check that the new content has landed 
   Clipboard := text 
   ClipWait ; wait until clipboard contains data 
   Send, ^v 
   PasteWait()
   Clipboard := tmp_clip ; restore the clipboard 
   ClipWait
}

PasteThis("Hello World")
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Clipboard: 'PasteWait': wait for paste to complete

05 Apr 2019, 11:14

Yes, that looks right. One thing to mention is to consider putting in a check to prevent an infinite loop.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
freespacing
Posts: 150
Joined: 28 Sep 2016, 11:14
Contact:

Re: Clipboard: 'PasteWait': wait for paste to complete

05 Apr 2019, 11:32

jeeswg wrote:
05 Apr 2019, 11:14
One thing to mention is to consider putting in a check to prevent an infinite loop.
Okay, that I can handle. :)

Pasting the timeout version below in case it helps someone.
However, the function still pastes the wrong thing for me, so for the time being I've gone back to Sleep, 500
Maybe a Win10 x64 thing?
In the meantime, I'll be watching hoping for the miracle solution to appear. Huge thanks for your patience and support, @jeeswg, regardless of what we accomplished today it means a lot.

Code: Select all

PasteWait(timeout=1000){ ; (not working yet for at least one user on Win10 x64)
    ; https://www.autohotkey.com/boards/viewtopic.php?f=5&t=37209&p=171360#p271287
    start_tick := A_TickCount ; time since last reboot in milliseconds
    while DllCall("user32\GetOpenClipboardWindow", "Ptr") {
        if(A_TickCount - start_tick) > timeout {
            Break
            }
    	Sleep, 50
        }
}
freespacing
Posts: 150
Joined: 28 Sep 2016, 11:14
Contact:

Re: Clipboard: 'PasteWait': wait for paste to complete

05 Apr 2019, 11:36

By the way forgot to say that I tried @SL5's version but that it seems to rely an an AHK function called DynaRun().
User avatar
SL5
Posts: 879
Joined: 12 May 2015, 02:10
Contact:

Re: Clipboard: 'PasteWait': wait for paste to complete

06 Apr 2019, 01:57

freespacing wrote:
05 Apr 2019, 11:36
By the way forgot to say that I tried @SL5's version but that it seems to rely an an AHK function called DynaRun().
its in v2 https://hotkeyit.github.io/v2/docs/commands/DynaRun.htm#Parameters
and you could find in some other scripts. also into the source von AHK_Studio
freespacing
Posts: 150
Joined: 28 Sep 2016, 11:14
Contact:

Re: Clipboard: 'PasteWait': wait for paste to complete

06 Apr 2019, 04:12

SL5 wrote:
06 Apr 2019, 01:57
its in v2
Thank you for writing, @SL5!
Precisely because it's in V2 at the moment I don't think I can use your PasteWait function (all my scripts are in V1).

Or do you think there are small changes that would make your PasteWait work in v1?
Thanks in advance for your thoughts.
User avatar
rommmcek
Posts: 1473
Joined: 15 Aug 2014, 15:18

Re: Clipboard: 'PasteWait': wait for paste to complete

10 May 2021, 01:53

To avoid an arbitrary set of the Sleep:
Spoiler
[Edit]: Fixed typo and the code flow.
[Edit2]: The code does not work as it should, thanks to TheBeginner, so don't use it, therefore is now hidden.
User avatar
kunkel321
Posts: 1033
Joined: 30 Nov 2015, 21:19

Re: Clipboard: 'PasteWait': wait for paste to complete

13 May 2022, 17:29

Hi Folks,
Thanks for your work on this thread. I'll admit it's all a bit above my head... Still I'll ask: Can this be adapted to force my script to wait for Windows to finish typing text (rather than waiting for the clipboard) ? You can see my sample code viewtopic.php?f=76&t=104022&p=461980#p461980

When using SendMode Input, the script seems to zip past the different clipboard assignments. Note that a long Sleep command would probably work, but I never know how big the string of text will be.

Hmmm... Should I set the length of the string to var, then have Sleep, %var% ?? Seems kludgey.
ste(phen|ve) kunkel
User avatar
boiler
Posts: 16900
Joined: 21 Dec 2014, 02:44

Re: Clipboard: 'PasteWait': wait for paste to complete

14 May 2022, 04:14

As I mentioned in the other thread, the reason it sends the third variable’s text twice has only to do with you immediately assigning new text to the clipboard after the first paste. It is not because of how long the Send command takes to send your first piece of text.

The issue as you described it makes that clear. The first piece of text finishes being sent without interruption, then the third piece of text is sent twice. That is exactly what is expected to happen because of the lack of the delay after the paste of your second piece of text regardless of what happened before it. You are wasting your time trying to figure out how to delay varying amounts of time based on of the length of text sent by the Send command.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 208 guests