0

我当前的代码从 10 到 56 读取用户输入的 2 位数字

有没有办法可以将最后一个用户输入保存到变量中,以便稍后在代码中的某个位置使用它?

这是我的代码

如果用户输入 11,我想将 11 保存在 tht 变量中,以便以后可以使用它如果用户输入 21,我希望它将 21 存储在该变量中

1::
Input Key, L1


if Key=1
            {
             ; do code
}
if Key=2
            {
             ; do code
}
return

2::
Input Key, L1


if Key=1
            {
             ; do code
}
if Key=2
            {
             ; do code
}
return

编辑代码

myVar=0

#o::
MsgBox %myVar% - 1 

return

因此,如果用户输入 11,它将设置 myVar = 11。然后我尝试对 myVar - 1 进行 subtrace 并尝试在 MsgBox 中打印,但它没有?它向我显示 11 - 1 而不是 11 减 1 我将使用 Send, %myVar% - 1 稍后代替 MsgBox。

谢谢

4

2 回答 2

0

There are 2 types of assignment statements in autohotkey: = & :=

  1. line1 = %g_number% - 1 ;this is a string

  2. line2 := g_number - 1 ;this is an expression

if g_number = 5

line1 will evaluate to 5 - 1 and line2 will evaulate to 4.

full code

#SingleInstance Force
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

g_number = 0

keys := "0123456789"

Loop, parse, keys
{
    Hotkey, %A_LoopField%, NumberKeyAction
}

return

#o::
    line1 = %g_number% - 1
    line2 := g_number - 1
    msgbox, %line1%`n%line2%
    return

NumberKeyAction:
    Input second_key, L1
    number = %A_ThisHotkey%%second_key%
    tooltip, %number%
    g_number := number
    return
于 2013-01-13T10:32:20.973 回答
0

我猜你需要按下按键的组合,如果按下 1,然后按下 2 来做你想要得到 12 的事情,等等。

只需使用全局变量

global myVar

-> 像这样定义的变量可以在程序的任何地方访问。

然后在你的一系列 If 句子中定义 myVar 。

1::
    Input Key, L1


    if Key=1
    {
        myVar:=11
        ; do code
    }
return
于 2013-01-13T00:09:29.553 回答