Page 1 of 1

Pasting strings does not always work

Posted: 23 Sep 2019, 01:47
by FredLabosch
Hi all,

I couldn't find anything about this issue in this forum and I hope this is the right place:

I have a simple script with which I want to past a line of text into any editor or application (see below). In most cases the text is only pasted after several trials (up to 10 times, I guess). The goal of this script is also to preserve the clipboard content. This leads to often this content being pasted instead of the desired line. I also tried to paste the line letter by letter but this is not helpful since I also want to paste longer texts. I also tried to enter several Wait instructions in the script, but this did also not help. Do you have any ideas what is wrong here?

I am using AHK v 1.1.30.03 on a Lenovo P50 with Win10.

The script I am using:

Code: Select all

; Use Ctrl + Shift + F6 to paste default line
^+F6::
	ClipSaved := ClipboardAll
	
	Clipboard = Some line of text to be pasted.
	
	Send ^v
	Clipboard := ClipSaved
	
	ClipSaved = ; Free Memory
return
Thanks,
FreadLabosch

Re: Pasting strings does not always work

Posted: 23 Sep 2019, 03:53
by YoucefHam
Hi there

check if the program have controls, so you can use ControlSetText.

Re: Pasting strings does not always work

Posted: 23 Sep 2019, 04:02
by swagfag
have u tried ClipWait
note: clipboard has to have been empty for clipwait to work

Re: Pasting strings does not always work

Posted: 23 Sep 2019, 04:07
by FredLabosch
YoucefHam wrote:
23 Sep 2019, 03:53
check if the program have controls, so you can use ControlSetText.
Hi,

thanks for the reply. Unfortunately, the program I want to use this with does not have a control in this subfield I want to enter the text. I tried with Window Spy and the "Focused Control" section is empty.

Re: Pasting strings does not always work

Posted: 23 Sep 2019, 04:09
by FredLabosch
swagfag wrote:
23 Sep 2019, 04:02
have u tried ClipWait
note: clipboard has to have been empty for clipwait to work
Yes, I have tried to put ClipWait on several positions but it did not help. Also, I need to preserve the original Clipboard content.

Re: Pasting strings does not always work

Posted: 23 Sep 2019, 05:05
by YoucefHam
What is the program name, so I will try it, if possibly.

Re: Pasting strings does not always work

Posted: 23 Sep 2019, 05:34
by FredLabosch
This is an internal tool I am using. However, this behavior also can be seen in any editor, e.g. in Notepad++ or Windows Editor. While this in turn would have a control, it would not help me with my internal software.

Re: Pasting strings does not always work

Posted: 23 Sep 2019, 07:40
by teadrinker
Try adding Sleep, 200 after Send ^v. Also you could try running the script as admin.

Re: Pasting strings does not always work

Posted: 23 Sep 2019, 07:52
by Chunjee
SendInput {Ctrl down}v{Ctrl up} works a little more reliably in my limited experience if the problem is with the paste aspect.

Re: Pasting strings does not always work

Posted: 23 Sep 2019, 08:25
by boiler
Edit: I see my input was already incorporated.

Re: Pasting strings does not always work

Posted: 23 Sep 2019, 08:48
by sinkfaze
Ideally you'd like to run a loop to check that the variables are changing at each step, but that (for me) just makes the script more burdensome to manage than just seeding each step with an arbitrary sleep time. At worst your hotkey will take a little over a second to execute, and if it works you can experiment to find where you can push the sleep time down:

Code: Select all

^+F6::
	ClipSaved :=	ClipboardAll
	Sleep, 250
	Clipboard = Some line of text to be pasted.
	Sleep, 250
	Send ^v
	Sleep, 250
	Clipboard := ClipSaved
	Sleep, 250
	ClipSaved = ; Free Memory
return

Re: Pasting strings does not always work

Posted: 23 Sep 2019, 09:31
by swub
Do you have other AutoHotkey apps running at the same time? Like I run Lintalist and CL3 at the same time and sometimes they clobber each other. Just a thought.

Re: Pasting strings does not always work

Posted: 01 Oct 2019, 05:59
by FredLabosch
sinkfaze wrote:
23 Sep 2019, 08:48
Ideally you'd like to run a loop to check that the variables are changing at each step, but that (for me) just makes the script more burdensome to manage than just seeding each step with an arbitrary sleep time. At worst your hotkey will take a little over a second to execute, and if it works you can experiment to find where you can push the sleep time down:

Code: Select all

^+F6::
	ClipSaved :=	ClipboardAll
	Sleep, 250
	Clipboard = Some line of text to be pasted.
	Sleep, 250
	Send ^v
	Sleep, 250
	Clipboard := ClipSaved
	Sleep, 250
	ClipSaved = ; Free Memory
return
Thank you for the input! So far it works, although I have not tried it over a longer time.
1 sec for the whole process seems a bit long, but I am trying different sleep values now.

Re: Pasting strings does not always work

Posted: 01 Oct 2019, 06:00
by FredLabosch
swub wrote:
23 Sep 2019, 09:31
Do you have other AutoHotkey apps running at the same time? Like I run Lintalist and CL3 at the same time and sometimes they clobber each other. Just a thought.
No, none that I know of. Good point though.

Re: Pasting strings does not always work

Posted: 01 Oct 2019, 06:58
by Blackholyman
adding sleep after the key input send ^v Seems to always work, as the other parts of the code is controlled, only how long the program that gets the ctrl+v takes to process that input is unknown so a sleep there is the best option

Code: Select all

^+F6::
ClipSaved := ClipboardAll
Clipboard := "Some line of text to be pasted."
Send ^v
Sleep, 300
Clipboard := ClipSaved
ClipSaved := "" ; Free Memory
return

Re: Pasting strings does not always work

Posted: 01 Oct 2019, 08:21
by boiler
You might try Clip() by @berban who claims it has has the right amount of sleep based on many years of experience to work reliably:

Code: Select all

^+F6::Clip("Some line of text to be pasted.")

; Clip() - Send and Retrieve Text Using the Clipboard
; by berban - updated February 18, 2019
; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=62156
Clip(Text="", Reselect="")
{
	Static BackUpClip, Stored, LastClip
	If (A_ThisLabel = A_ThisFunc) {
		If (Clipboard == LastClip)
			Clipboard := BackUpClip
		BackUpClip := LastClip := Stored := ""
	} Else {
		If !Stored {
			Stored := True
			BackUpClip := ClipboardAll ; ClipboardAll must be on its own line
		} Else
			SetTimer, %A_ThisFunc%, Off
		LongCopy := A_TickCount, Clipboard := "", LongCopy -= A_TickCount ; LongCopy gauges the amount of time it takes to empty the clipboard which can predict how long the subsequent clipwait will need
		If (Text = "") {
			SendInput, ^c
			ClipWait, LongCopy ? 0.6 : 0.2, True
		} Else {
			Clipboard := LastClip := Text
			ClipWait, 10
			SendInput, ^v
		}
		SetTimer, %A_ThisFunc%, -700
		Sleep 20 ; Short sleep in case Clip() is followed by more keystrokes such as {Enter}
		If (Text = "")
			Return LastClip := Clipboard
		Else If ReSelect and ((ReSelect = True) or (StrLen(Text) < 3000))
			SendInput, % "{Shift Down}{Left " StrLen(StrReplace(Text, "`r")) "}{Shift Up}"
	}
	Return
	Clip:
	Return Clip()
}

Re: Pasting strings does not always work

Posted: 14 Oct 2019, 00:38
by FredLabosch
Hi all,

thank you all for helping me out here with quick replies and valuables inputs! :clap: :clap: :clap:

I am now using this code and it always works. I got it down to 50ms for the delay which is barely noticeable.

Code: Select all

global SleepDelay := 50

^+F6::
	ClipSaved :=	ClipboardAll
	Sleep, %SleepDelay%
	Clipboard = Some line of text to be pasted.
	Sleep, %SleepDelay%
	Send ^v
	Sleep, %SleepDelay%
	Clipboard := ClipSaved
	Sleep, %SleepDelay%
	ClipSaved = ; Free Memory
return