Pushing lines from Loop, Read into array?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
nompynuthead

Pushing lines from Loop, Read into array?

25 Feb 2018, 06:22

I'm relatively new to AHK, and I just haven't been able to find out what's going wrong here.
Essentially, I have a program that is meant to detect when the user physically clicks, take a pair of coordinates from a text file, and then virtually click the mouse at those coordinates. These coordinates are just two numbers in a text file, separated by a newline, e.g.:
100
200

My problem is, I can't seem to get this pair of coordinates to load into the mouseCoords array. They just aren't pushed to it at all. I don't know if this is a problem with my Loop statement or my mouseCoords.Push(A_LoopReadLine) statement, but I have tried for hours and I just can't seem to get it to work. Can anyone help to fix?

Code: Select all

CoordMode, Mouse

physClick = False
LButton::
    GetKeyState, State, LButton, P
    If State = D
        physClick = True

return

LButton Up::
    If physClick = True
        mouseCoords := []
        Loop, Read, mousepos.txt
        {
            mouseCoords.Push(A_LoopReadLine)
        }


        physClick = False
        Send {Click, mouseCoords[1], mouseCoords[2]}

return

!z::
    ExitApp
return
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Pushing lines from Loop, Read into array?

25 Feb 2018, 07:07

You can't use expressions or objects in a Click statement, so you have to use a pseudo-array and use % around the variables. Plus, all that stuff with the physClick, is that just so the virtual click doesn't trigger the LButton hotkey? That's what the $ option on hotkeys is for. I believe your script can be simplified to this:

Code: Select all

CoordMode, Mouse

$LButton::
	Loop, Read, mousepos.txt
		mouseCoords%A_Index% := A_LoopReadLine
	Click, %mouseCoords1%, %mouseCoords2%
return

!z::
    ExitApp
return
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Pushing lines from Loop, Read into array?

25 Feb 2018, 07:08

You need brackets {} around pieces of code that you want to stick together:

Code: Select all

CoordMode, Mouse

physClick = False
LButton::
    GetKeyState, State, LButton, P
    If State = D
        physClick = True

return

LButton Up::
    If physClick = True {
        mouseCoords := []
        Loop, Read, mousepos.txt
        {
            mouseCoords.Push(A_LoopReadLine)
        }


        physClick = False
        Send % "{Click, " . mouseCoords[1] . "," .  mouseCoords[2] . "}" ; the . for string concenation
    }

return

!z::
    ExitApp
return
Also you need to force expression mode in the Send
Recommends AHK Studio
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Pushing lines from Loop, Read into array?

25 Feb 2018, 07:09

boiler wrote:You can't use expressions or objects in a Click statement, so you have to use a pseudo-array and use % around the variables. Plus, all that stuff with the physClick, is that just so the virtual click doesn't trigger the LButton hotkey? That's what the $ option on hotkeys is for. I believe your script can be simplified to this:

Code: Select all

CoordMode, Mouse

$LButton::
	Loop, Read, mousepos.txt
		mouseCoords%A_Index% := A_LoopReadLine
	Click, %mouseCoords1%, %mouseCoords2%
return

!z::
    ExitApp
return
Quite wrong - you can always force expression mode.
Recommends AHK Studio
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Pushing lines from Loop, Read into array?

25 Feb 2018, 07:13

Wow, you seem pretty sure of yourself, nnnik. Care to amend your statement when it comes to the Click command?
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Pushing lines from Loop, Read into array?

25 Feb 2018, 07:16

As nnnik says:

Code: Select all

$LButton::
	mouseCoords:= []
	Loop, Read, mousepos.txt
		mouseCoords.Push(A_LoopReadLine)
	Send % "{Click, " . mouseCoords[1] . "," .  mouseCoords[2] . "}" 
return
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Pushing lines from Loop, Read into array?

25 Feb 2018, 07:18

That is an expression of the Send command. His general statement that you can always force expression mode is not correct when it comes to the Click command.
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Pushing lines from Loop, Read into array?

25 Feb 2018, 07:20

I didn't say that what he did couldn't work. But in my code, I used Click, and that's why I used a pseudo array. His general statement was quite wrong.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Pushing lines from Loop, Read into array?

25 Feb 2018, 10:47

You can force an expression for click's one and only parameter, eg, click % x " " y. The documentation isn't very good in this case.

Cheers.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Theda and 278 guests