如何在自动热键中传播变量?



在JavaScript中,我们使用展开操作符来展开数组中的项,例如

const arr = [1, 2, 3]
console.log(...arr) // 1 2 3

我想在AHK中达到类似的效果:

Position := [A_ScreenWidth / 2, A_ScreenHeight]
MouseMove Position ; 👈 how to spread it?

在AHK中没有扩展语法,但是有一些替代方法:

对于大数组,可以使用:

position := [A_ScreenWidth / 2, A_ScreenHeight]
Loop,% position.Count()
MsgBox % position[A_Index] ; show a message box with the content of any value

position := [A_ScreenWidth / 2, A_ScreenHeight]
For index, value in position
MsgBox % value ; show a message box with the content of any value

在你的例子中,可以是:

position := [A_ScreenWidth / 2, A_ScreenHeight]
MouseMove, position[1], position[2]

这将把你的鼠标移动到屏幕的底部中间。

为了避免小数,您可以使用Floor(),Round(),Ceil()函数,例如:

position := [ Floor( A_ScreenWidth / 2 ), Round( A_ScreenHeight ) ]
Loop,% position.Count()
MsgBox % position[A_Index] ; show a message box with the content of any value

最新更新