Page 1 of 1

SendInput has problem with A_Index

Posted: 25 May 2017, 17:19
by Tobor D
[Moderator's note: Topic moved from Bug Reports.]

Good day.
I made a script it is easiest clipboard manager. It saves your text from clipboard to file and then using FileReadLine with A_Index to send each line in rotation.

Code: Select all

#SingleInstance force

Numpad1::
Send ^c
FileAppend, %clipboard% `n, %a_temp%\ClipBoardM.txt
Return

Numpad2::
Loop
{

 FileReadLine, cpline, %a_temp%\ClipBoardM.txt, %A_Index%
   if  cpline =
   {
   MsgBox , , ,Choose a text, 1
   goto, endx
   }
     if ErrorLevel
	 {
	    Sendinput, {Numpad2}
        goto, endx
	 } 
Send, %cpline%{BS}
KeyWait, Numpad2, D
}
endx:
Return

Numpad3::
FileDelete, %a_temp%\ClipBoardM.txt
MsgBox , , ,Cleared, 1
Reload
Return
The problem in this line:

Code: Select all

Send, %cpline%{BS}

I wish to use SendInput here because it faster then Send but...
You can not use SendInput it gaves wrong result instead of Send. With SendInput the result will be a whole lines from file but not an each lines in rotation.
To clearly understand what i mean I advice you to chek this script and see which result will be if you change Send on SendInput.

Re: SendInput has problem with A_Index

Posted: 25 May 2017, 20:44
by Xtra
Try using ClipWait before writing the file.

Code: Select all

Numpad1::
Send ^c
Clipwait, 1
FileAppend, %clipboard% `n, %a_temp%\ClipBoardM.txt
Return
This should be in ask for help...

Re: SendInput has problem with A_Index

Posted: 25 May 2017, 23:43
by Tobor D
Xtra wrote:This should be in ask for help...
Well if its works for you its strange because it didnt works for me...

Re: SendInput has problem with A_Index

Posted: 26 May 2017, 01:22
by Xtra
What i was pointing out is you are assuming the clipboard has content when writing to file.

Re: SendInput has problem with A_Index

Posted: 26 May 2017, 01:31
by IMEime
No bugs at all.

Code: Select all

myVar := "FooBoo"
Send, %myVar%{BS 3}
SendInput, %myVar%{BS 3}  	;   "FooFoo"

Re: SendInput has problem with A_Index

Posted: 26 May 2017, 02:58
by Tobor D
You guys probably didn`t get what i mean. You should figure out by checking full script.
Check it without remark this line:

Code: Select all

Send, %cpline%{BS}
And then check with changes:

Code: Select all

Sendinput, %cpline%{BS}
IMEime wrote:No bugs at all.

Code: Select all

myVar := "FooBoo"
Send, %myVar%{BS 3}
SendInput, %myVar%{BS 3} ; "FooFoo"
I dont see the conection with your messege and my:
With SendInput the result will be a whole lines from file but not an each lines in rotation.

Re: SendInput has problem with A_Index

Posted: 26 May 2017, 03:34
by IMEime
No bugs at all.

Code: Select all

SendInput, % cpline "`n Surprise `n"

Re: SendInput has problem with A_Index

Posted: 26 May 2017, 06:30
by Tobor D
IMEime wrote:No bugs at all.

Code: Select all

SendInput, % cpline "`n Surprise `n"
Well if i saving two random words like "home" and "tube" and then i using your "Not bug at all" codeline i got this:
"
tube
Surprise
home
Surprise
"
(Here i pushed Numpad2 one time)

But i if change Sendinput on Send here in your codeline like this:

Code: Select all

Send, % cpline "`n Surprise `n"
I will get what i need (almost)

When i push Numpad2 first time i get:
"home
Surprise
"
And when i push second time:
"
tube
Surprise
".

Re: SendInput has problem with A_Index  Topic is solved

Posted: 27 May 2017, 00:05
by lexikos
The bug is in your script.

Both Send and SendInput produce the wrong result. If there are any differences, it is only because of timing.

When you press Numpad2, the loop starts. If Send or SendInput takes x ms, you must release Numpad2 less than x ms after pressing it, otherwise the loop will iterate multiple times even though you've only pressed the key once. If you press the key for 50 ms and Send takes 55 ms while SendInput takes 45 ms, SendInput will cause two iterations while Send will cause one.

Instead of just waiting for NumPad2 to be down (which will not wait at all if it is still/already down), you may wait for it to be down and then wait for it to be up:

Code: Select all

KeyWait, Numpad2, D
KeyWait, Numpad2
Alternatively, you may remove the loop and use a counter (in place of A_Index) which is incremented each time the hotkey is called.

After you send ^c, you should give the active window some time to process it and change the clipboard content. Otherwise, the script may append the old clipboard contents.

Note that %clipboard% `n writes a space at the end of the line, which may be unintended.

Re: SendInput has problem with A_Index

Posted: 27 May 2017, 20:22
by Tobor D
lexikos
Thank you master. :)
Alternatively, you may remove the loop and use a counter
How to use this counter? Is it some kind off function?

Re: SendInput has problem with A_Index

Posted: 28 May 2017, 00:56
by lexikos
Use variables and math.