Pasting strings does not always work

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
FredLabosch
Posts: 7
Joined: 23 Sep 2019, 01:31

Pasting strings does not always work

23 Sep 2019, 01:47

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
User avatar
YoucefHam
Posts: 372
Joined: 24 Aug 2015, 12:56
Location: Algeria
Contact:

Re: Pasting strings does not always work

23 Sep 2019, 03:53

Hi there

check if the program have controls, so you can use ControlSetText.
:wave: There is always more than one way to solve a problem. ;)
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Pasting strings does not always work

23 Sep 2019, 04:02

have u tried ClipWait
note: clipboard has to have been empty for clipwait to work
FredLabosch
Posts: 7
Joined: 23 Sep 2019, 01:31

Re: Pasting strings does not always work

23 Sep 2019, 04:07

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.
FredLabosch
Posts: 7
Joined: 23 Sep 2019, 01:31

Re: Pasting strings does not always work

23 Sep 2019, 04:09

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.
User avatar
YoucefHam
Posts: 372
Joined: 24 Aug 2015, 12:56
Location: Algeria
Contact:

Re: Pasting strings does not always work

23 Sep 2019, 05:05

What is the program name, so I will try it, if possibly.
:wave: There is always more than one way to solve a problem. ;)
FredLabosch
Posts: 7
Joined: 23 Sep 2019, 01:31

Re: Pasting strings does not always work

23 Sep 2019, 05:34

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.
teadrinker
Posts: 4565
Joined: 29 Mar 2015, 09:41
Contact:

Re: Pasting strings does not always work

23 Sep 2019, 07:40

Try adding Sleep, 200 after Send ^v. Also you could try running the script as admin.
User avatar
Chunjee
Posts: 1601
Joined: 18 Apr 2014, 19:05
Contact:

Re: Pasting strings does not always work

23 Sep 2019, 07:52

SendInput {Ctrl down}v{Ctrl up} works a little more reliably in my limited experience if the problem is with the paste aspect.
User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: Pasting strings does not always work

23 Sep 2019, 08:25

Edit: I see my input was already incorporated.
Last edited by boiler on 23 Sep 2019, 08:51, edited 1 time in total.
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: Pasting strings does not always work

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
swub
Posts: 19
Joined: 25 Feb 2019, 09:16

Re: Pasting strings does not always work

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.
FredLabosch
Posts: 7
Joined: 23 Sep 2019, 01:31

Re: Pasting strings does not always work

01 Oct 2019, 05:59

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.
FredLabosch
Posts: 7
Joined: 23 Sep 2019, 01:31

Re: Pasting strings does not always work

01 Oct 2019, 06:00

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.
User avatar
Blackholyman
Posts: 1293
Joined: 29 Sep 2013, 22:57
Location: Denmark
Contact:

Re: Pasting strings does not always work

01 Oct 2019, 06:58

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
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:
User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: Pasting strings does not always work

01 Oct 2019, 08:21

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()
}
FredLabosch
Posts: 7
Joined: 23 Sep 2019, 01:31

Re: Pasting strings does not always work

14 Oct 2019, 00:38

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

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], joefiesta, leothlon and 101 guests