SendInput has problem with A_Index Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Tobor D
Posts: 10
Joined: 07 Apr 2017, 20:35

SendInput has problem with A_Index

25 May 2017, 17:19

[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.
User avatar
Xtra
Posts: 1356
Joined: 02 Oct 2015, 12:15

Re: SendInput has problem with A_Index

25 May 2017, 20:44

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...
Tobor D
Posts: 10
Joined: 07 Apr 2017, 20:35

Re: SendInput has problem with A_Index

25 May 2017, 23:43

Xtra wrote:This should be in ask for help...
Well if its works for you its strange because it didnt works for me...
User avatar
Xtra
Posts: 1356
Joined: 02 Oct 2015, 12:15

Re: SendInput has problem with A_Index

26 May 2017, 01:22

What i was pointing out is you are assuming the clipboard has content when writing to file.
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: SendInput has problem with A_Index

26 May 2017, 01:31

No bugs at all.

Code: Select all

myVar := "FooBoo"
Send, %myVar%{BS 3}
SendInput, %myVar%{BS 3}  	;   "FooFoo"
Tobor D
Posts: 10
Joined: 07 Apr 2017, 20:35

Re: SendInput has problem with A_Index

26 May 2017, 02:58

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.
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: SendInput has problem with A_Index

26 May 2017, 03:34

No bugs at all.

Code: Select all

SendInput, % cpline "`n Surprise `n"
Tobor D
Posts: 10
Joined: 07 Apr 2017, 20:35

Re: SendInput has problem with A_Index

26 May 2017, 06:30

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
".
lexikos
Posts: 6404
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: SendInput has problem with A_Index  Topic is solved

27 May 2017, 00:05

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.
Tobor D
Posts: 10
Joined: 07 Apr 2017, 20:35

Re: SendInput has problem with A_Index

27 May 2017, 20:22

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?
lexikos
Posts: 6404
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: SendInput has problem with A_Index

28 May 2017, 00:56

Use variables and math.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], flyingDman, Google [Bot], WilburBr and 136 guests