在Autohotkey中创建多维数组,该数组根据用户输入进行更新
Autohotkey中的一般问题
,我很难将值附加到array
对象,该对象开始空,然后在用户执行某些操作时被值填充。
下面复制的脚本
是我脚本的小型可重复版本。在其中,目标是为numpadadd
键,将当前鼠标位置添加到array
nater 称为list_of_mouse_positions
。添加几点后,用户可以使用numpad0
键来查看迄今为止存储的位置列表。
;---------------;
; GENERAL SETUP ;
;---------------;
coordmode Mouse, screen
; Creates an empty array. This will store the mouse positions.
list_of_mouse_positions := Array()
;-----------;
; FUNCTIONS ;
;-----------;
; Function used to add the current mouse position to the global
; list of mouse positions. The `x` parameter serves no purpose.
AddLocation(x)
{
MouseGetPos, xpos, ypos
mouse_pos := Array()
mouse_pos[1] := xpos
mouse_pos[2] := ypos
list_of_mouse_positions.Push(mouse_pos)
}
;-------------;
; KEYBINDINGS ;
;-------------;
; Adds the current mouse position to the
NumpadAdd::
AddLocation(0)
return
; Checking the mouse positions stored so far
NumPad0::
MsgBox, %list_of_mouse_positions%
return
预期输出与实际输出
运行脚本并击中numpadadd
键以捕获几个坐标,我点击了numpad0
键。我期望出现的是一个包含所有坐标的消息框。但是,出现的只是一个空的消息框。
特定问题
如何通过将新的行附加到其上,每个行都会存储两个新值?
注意list_of_mouse_positions
的预期维度,
我希望list_of_of_mouse_positions
变量变量为多维数组(nx 2)所有x值,第二列包含所有y值。
例如,假设用户“捕获”以下鼠标位置的坐标:
- (100,200)
- (400,100)
- (600,150)
在这种情况下,list_of_of_mouse_positions
的结构将是如下:
list_of_mouse_positions[1,1] = 100
list_of_mouse_positions[1,2] = 200
list_of_mouse_positions[2,1] = 400
list_of_mouse_positions[2,2] = 100
list_of_mouse_positions[3,1] = 600
list_of_mouse_positions[3,2] = 150
General problem
In AutoHotKey, I'm having a hard time appending values to an Array
object that starts off empty and then gets filled up with values as the user performs certain actions.
The script
Copied below is a small reproducible version of my script. In it, the goal is for the NumpadAdd
key to add the current mouse position to the Array
called list_of_mouse_positions
. After adding a few points, the user can use the Numpad0
key to look at the list of positions stored thus far.
;---------------;
; GENERAL SETUP ;
;---------------;
coordmode Mouse, screen
; Creates an empty array. This will store the mouse positions.
list_of_mouse_positions := Array()
;-----------;
; FUNCTIONS ;
;-----------;
; Function used to add the current mouse position to the global
; list of mouse positions. The `x` parameter serves no purpose.
AddLocation(x)
{
MouseGetPos, xpos, ypos
mouse_pos := Array()
mouse_pos[1] := xpos
mouse_pos[2] := ypos
list_of_mouse_positions.Push(mouse_pos)
}
;-------------;
; KEYBINDINGS ;
;-------------;
; Adds the current mouse position to the
NumpadAdd::
AddLocation(0)
return
; Checking the mouse positions stored so far
NumPad0::
MsgBox, %list_of_mouse_positions%
return
Expected Output vs Actual Output
After running the script and hitting the NumpadAdd
key to capture a couple of coordinates, I hit the Numpad0
key. What I expected to show up was a Message Box containing all of the coordinates. However, what showed up instead was just an empty message box.
Specific question
How can I update an Array
object (in my case, the list_of_mouse_positions
variable) by appending new rows to it, each one of which stores two new values?
Note about the expected dimensions of list_of_mouse_positions
I expect the list_of_mouse_positions
variable to be a multidimensional array of dimensions (n x 2)
, where the first column contains all of the x values, and the second column contains all of the y values.
For example, suppose the user "captured" the coordinates of the following mouse positions:
- (100, 200)
- (400, 100)
- (600, 150)
In this case, the structure of the list_of_mouse_positions
would be as follows:
list_of_mouse_positions[1,1] = 100
list_of_mouse_positions[1,2] = 200
list_of_mouse_positions[2,1] = 400
list_of_mouse_positions[2,2] = 100
list_of_mouse_positions[3,1] = 600
list_of_mouse_positions[3,2] = 150
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更正的代码以匹配您的新代码,但使用超级全球编辑(请参见下面的DOC)
关于您可能想做的简单性,或者您不想做的简单性,其中包括一些包含的评论:
Corrected code to match your new code, but with super-global edit (see doc quote below)
with some included comments on your code with regards to simplicity you may want to do, or not as you desire: