"Clipboard :=" does not always empty the clipboard immediately Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
freespacing
Posts: 150
Joined: 28 Sep 2016, 11:14
Contact:

"Clipboard :=" does not always empty the clipboard immediately

04 Apr 2019, 06:59

EDIT: problem seems to be solved thanks to the help of many people.

The problem was not about emptying the clipboard, but about the speed of the Paste that appeared a few lines below.

In the end the solution that seemed to work best was that suggested by jeeswg here, and embodied in the SafePaste() function.

Sometimes when I empty the clipboard with Clipboard := then later paste the "new clipboard" via Send, ^v, I end up with the old clipboard. In other words, Clipboard := does not always empty the keyboard immediately.

I can mitigate that with a Sleep, 200 but that's not always reliable (and potentially wasteful).

What would be the recommended loop to empty the keyboard and check that it is in fact empty?

Thanks!

Moderator Note: Edited post title to be more descriptive of the problem. ~ sinkfaze
Last edited by freespacing on 07 Apr 2019, 12:30, edited 2 times in total.
Rohwedder
Posts: 7645
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: "Clipboard :=" does not always empty the clipboard immediately

04 Apr 2019, 07:33

Hallo,
replace:

Code: Select all

Clipboard :=
by:

Code: Select all

While(ClipBoard:="")
	Sleep 0
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: "Clipboard :=" does not always empty the clipboard immediately

04 Apr 2019, 07:40

I always write Clipboard = ; Erase clipboard and never have problems.

Cheers!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: "Clipboard :=" does not always empty the clipboard immediately

04 Apr 2019, 08:16

- Are you saying that this sometimes pastes text!?

Code: Select all

Clipboard := ""
SendInput, ^v
- Perhaps you could mention what programs you're using.
- E.g. NetBeans had (has?) some problems with setting the clipboard.

- A side note re. IDLE:
- Also, I did some experiments with Python's IDLE, and noticed that sometimes it reads from a 'local clipboard' instead of the regular clipboard.
- Setting the Clipboard variable always worked, but modifying individual clipboard formats, something that worked on other programs, didn't work on IDLE. If GetClipboardOwner reported IDLE as the last owner, I had to use the Clipboard variable, otherwise, I could modify just part of the clipboard, the text format, via SetClipboardData (and half-a-dozen other Winapi functions).
- I succeeded in forcing tab indentation, instead of 4-space indentation, when tab is pressed.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Klarion
Posts: 176
Joined: 26 Mar 2019, 10:02

Re: "Clipboard :=" does not always empty the clipboard immediately

04 Apr 2019, 09:19

Yes
You are absolutely right
The Clipboard is quite slow animal, it makes trouble lots of times

So
You have to have Sleep always

If you want you could use a loop (or another workaround) too, for sure
I'd rather recommend sleep like this

Code: Select all

Clipboard =
Sleep, 33
That works 100%
I promise
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: "Clipboard :=" does not always empty the clipboard immediately

04 Apr 2019, 09:51

Clipboard := isn't valid, either use Clipboard := "" or Clipboard =.

Cheers.
freespacing
Posts: 150
Joined: 28 Sep 2016, 11:14
Contact:

Re: "Clipboard :=" does not always empty the clipboard immediately

04 Apr 2019, 17:54

Hi everyone,

Thank you for the helpful hints.
This problem of the Clipboard not getting to empty fast enough arises in many text editors where I am running scripts (EditPad, PyCharm, even within text boxes of web browsers). It's not all the time, just some of the time, and this unpredictability is a problem.

Following the suggestions and more threads, I tried all this:

Code: Select all

    ; start off empty to allow ClipWait to detect when text has arrived.
    Clipboard = 
    ; it can take time for the Clipboard to clear  
   /* not working
    loop
        {
        if clipboard = 
           break
        }
   */ 

    ; While(Clipboard != "") ; not working
      While(Clipboard) ; not working
        Sleep, 50

	Clipboard := text
	ClipWait ; wait until clipboard contains data
	Send, ^v
But none of this seems to be working, as Ctrl + v still sometimes sends the old clipboard.

Would be very grateful for any suggestions.
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: "Clipboard :=" does not always empty the clipboard immediately

04 Apr 2019, 18:02

freespacing wrote:
04 Apr 2019, 17:54
Hi everyone,

Thank you for the helpful hints.
This problem of the Clipboard not getting to empty fast enough arises in many text editors where I am running scripts (EditPad, PyCharm, even within text boxes of web browsers). It's not all the time, just some of the time, and this unpredictability is a problem.

Following the suggestions and more threads, I tried all this:

Code: Select all

    ; start off empty to allow ClipWait to detect when text has arrived.
    Clipboard = 
    ; it can take time for the Clipboard to clear  
   /* not working
    loop
        {
        if clipboard = 
           break
        }
   */ 

    ; While(Clipboard != "") ; not working
      While(Clipboard) ; not working
        Sleep, 50

	Clipboard := text
	ClipWait ; wait until clipboard contains data
	Send, ^v
But none of this seems to be working, as Ctrl + v still sometimes sends the old clipboard.

Would be very grateful for any suggestions.
Try this:

Code: Select all

Clipboard := ""
While (Clipboard != "")
	Sleep, 10
MsgBox % "Clipboard is now cleared.`n`nIf this did not work, there should be text shown below:`n`n" Clipboard
If that doesn't work - there is something else wrong with your device / setup / programs you are interfacing with that is interferring with this procedure.
-TL
freespacing
Posts: 150
Joined: 28 Sep 2016, 11:14
Contact:

Re: "Clipboard :=" does not always empty the clipboard immediately

04 Apr 2019, 18:15

@TL Very interesting suggestion!

With what you said and this shorter message:

Code: Select all

    Clipboard := "" 
    While (Clipboard != "") 
        Sleep, 10 
    MsgBox %Clipboard%
the MsgBox shows nothing, but the Ctrl + v pastes the old clipboard!

At the moment a Sleep, 500 seems to solve it but that is a bit extreme (1980s-flavor latency).
Klarion
Posts: 176
Joined: 26 Mar 2019, 10:02

Re: "Clipboard :=" does not always empty the clipboard immediately

04 Apr 2019, 18:23

Because, simply, your process is wrong
you'd better write down all your process in every detail
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: "Clipboard :=" does not always empty the clipboard immediately

04 Apr 2019, 18:24

What about when you use space, or even 'abc', is there just a general issue with programs ignoring the clipboard (or ignoring it when it's blank)?

Code: Select all

Clipboard := " "
while (Clipboard != " ")
	Sleep, 10
MsgBox, % Clipboard
You could set up a ^v (Ctrl+V) hotkey for some programs, and then use this:

Code: Select all

SendInput, % "{Text}" Clipboard ;requires AHK v1.1.27+
You could also inspect the clipboard with NirSoft InsideClipboard.

Perhaps you could send a message to the program, that will update/empty what it views as the clipboard contents.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
TAC109
Posts: 1112
Joined: 02 Oct 2013, 19:41
Location: New Zealand

Re: "Clipboard :=" does not always empty the clipboard immediately

04 Apr 2019, 18:37

After the Send ^v are you using ClipWait? This is needed to ensure the clipboard has been refreshed by Windows before your script continues.

Edit:- Sorry, some confusion between ^v and ^c!
Last edited by TAC109 on 04 Apr 2019, 20:53, edited 1 time in total.
My scripts:-
XRef - Produces Cross Reference lists for scripts
ReClip - A Text Reformatting and Clip Management utility
ScriptGuard - Protects Compiled Scripts from Decompilation
I also maintain Ahk2Exe
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: "Clipboard :=" does not always empty the clipboard immediately

04 Apr 2019, 18:42

- Ah yes. There can be a problem if you try to set the clipboard contents just after sending Ctrl+V. You may send the new contents instead.
- I tried the following code on Notepad, and got 'def' once out of hundreds of attempts.

Code: Select all

q:: ;test set clipboard contents after send
Clipboard := "abc"
SendInput, ^v
Clipboard := "def"
return
- Here is an attempt to wait for the paste to complete:
Clipboard: 'PasteWait': wait for paste to complete - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=37209&p=171360#p171360
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
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: "Clipboard :=" does not always empty the clipboard immediately

04 Apr 2019, 20:19

TAC109 wrote:
04 Apr 2019, 18:37
After the Send ^v are you using ClipWait? This is needed to ensure the clipboard has been refreshed by Windows before your script continues.
This is incorrect - you use ClipWait to wait until your clipboard contains data. You would want to use ClipWait after you clear your clipboard and are awaiting new data to populate the clipboard.

Code: Select all

ClipWait

Waits until the clipboard contains data.

ClipWait [, SecondsToWait, WaitForAnyData]
freespacing wrote:
04 Apr 2019, 18:15
Perhaps an experiment would be to add onto the code I gave you and put ClipWait afterwards like this:

Code: Select all

Clipboard := ""
While (Clipboard != "")
	Sleep, 10
MsgBox % "Clipboard is now cleared.`n`n"
.	"If this did not work, there should be text shown below:`n`n"
.	 Clipboard
InitialTime := A_TickCount
ClipWait, 60
	if (ErrorLevel = 1)
		MsgBox % "No data or text populated your clipboard within the last minute"
TimeElapsed := A_TickCount - InitialTime
MsgBox % "If is text shown below, something other than AutoHotkey is filling your clipboard without you being aware of what it is."
.	"`n`nThe text show below is in your clipboard:`n`n`n" 
.	Clipboard 
.	"`n`n`nIt took " TimeElapsed " milliseconds for your clipboard to be populated with data again without your permission."
Good luck.
Last edited by Tigerlily on 04 Apr 2019, 21:00, edited 1 time in total.
-TL
iseahound
Posts: 1445
Joined: 13 Aug 2016, 21:04
Contact:

Re: "Clipboard :=" does not always empty the clipboard immediately

04 Apr 2019, 20:38

A sleep of 50 after sending ^v is standard. Seems to be a windows limitation.
freespacing
Posts: 150
Joined: 28 Sep 2016, 11:14
Contact:

"Ctrl + v" not sending the content of the Clipboard variable

05 Apr 2019, 09:52

Howdy everyone,
Thank you all very much for your helpful messages. Exciting news to report.

I have isolated the problem:
  • the Clipboard is emptying correctly, but
  • Ctrl + v pastes the old clipboard when I restore Clipboard with too short a sleep below Ctrl + v
  • a Sleep of 300 after the Send, ^v is not enough, 500 seems to do it

Code: Select all

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
The question becomes: Is there a way to avoid always wasting a Sleep, 500, in other words is there a way to guarantee that Ctrl + v has finished before resetting the clipboard?



Here is how I reached these conclusions.

Looking at all the suggestions, including that of using NirSoft's InsideClipboard, I made a number of tests culminating in this one, where the value of the final Sleep affects the rate of errors, with 500 seeming okay.

To make sure there is no weirdness testing the Clibpoard variable itself, instead I tested the length of a dummy variable which is concatenated with Clipboard, guaranteeing a different address in memory:

Code: Select all

PasteThis(text) ; use Ctrl+v instead of Send in order to avoid triggering auto-completion in certain environments
{
   tmp_clip := ClipboardAll ; preserve Clipboard
    dummy := ""
    While(StrLen(dummy) != 1) { ; retry
        Clipboard := "" 
        Sleep, 33 ; it either happens fast, or not at all
        dummy := "@" . Clipboard    ; dummy will have a different address in memory from Clipboard
        }
    MsgBox %dummy% ; This works and shows @, EVEN WHEN THE PASTING FAILS!
	Clipboard := text ; "ABC"
	ClipWait ; wait until clipboard contains data
	MsgBox %Clipboard% ; This works and shows ABC !
	Send, ^v ; PASTES THE WRONG THING when the value of Sleep on the next line is too low!
	Sleep, 100  ; prevents the clipboard from changing during the paste operation
			 ; a value of 500 seems to work... but is there a better way to ensure that Ctrl+v has finished?
	Clipboard := tmp_clip ; restore the clipboard
    ClipWait
}

Results when failing:
  • 1. MsgBox %dummy% shows the correct message ("@")
    2. MsgBox %Clipboard% shows the correct content that is supposed to be pasted ("ABC")
    3. The function pastes the OLD clipboard (!!!), not what was shown by MsgBox %Clipboard%
    4. NirSoft shows that the clipboard is the OLD clipboard
    5. Basically, what happens is that Clipboard := tmp_clip has restored the clipboard before pasting, even though that line is below the Ctrl+v
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: "Clipboard :=" does not always empty the clipboard immediately

05 Apr 2019, 10:08

- So it sounds like the same problem as my 'abc' and 'def' example above.
- And the 'PasteWait' script mentioned above could be a possible solution.
- In general, look for any visible difference, before/after pasting, e.g. caret position (A_CaretX/A_CaretY), cursor icon (A_Cursor), control text.
- You could also mention what control type you're pasting into. E.g. there may be alternatives to pasting for Edit/Scintilla controls.
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 :=" does not always empty the clipboard immediately

05 Apr 2019, 10:13

@jeeswg

Thank you, that sounds Spot On!

You mention a PasteWait script, but I don't see it on the thread.
Would you please do me a big favor and point me to the version you like?


Don't know why my Ctrl + F didn't work the first time!
Rohwedder
Posts: 7645
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: "Clipboard :=" does not always empty the clipboard immediately

05 Apr 2019, 11:23

Hallo.
perhaps:

Code: Select all

PasteThis(text) ; use Ctrl+v instead of Send in order to avoid triggering auto-completion in certain environments
{
	Static tmp_clip, tmp_clip2
	While, tmp_clip <> tmp_clip2
		Sleep, 10 ;restoration not yet finished
	tmp_clip := ClipboardAll ; preserve Clipboard
	ClipBoard := text
	While, tmp_clip2 <> text		
		tmp_clip2 := ClipBoard ;validate clipboard
	Send, ^v
	SetTimer, restoration, -500
	Return ;don't waste time waiting for restoration
	restoration:
	ClipBoard := tmp_clip ; restore the clipboard
	While, tmp_clip <> tmp_clip2		
		tmp_clip2 := ClipBoardAll ;validate clipboard
	tmp_clip:="", tmp_clip2:=""
	Return
}
freespacing
Posts: 150
Joined: 28 Sep 2016, 11:14
Contact:

Re: "Clipboard :=" does not always empty the clipboard immediately

05 Apr 2019, 12:07

@Rohwedder

Your function is working on my machine!
I don't understand it, but it works. Big big thanks...
:thumbup: :D :bravo:

One question since I don't understand it.
I see '500' mentioned. Is this a timeout? What happens if we reach 500 milliseconds?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen and 410 guests