如何在变量中存储最后的用户输入?

时间:2013-01-12 22:17:50

标签: autohotkey

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

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

这里与我的代码相同

如果用户输入11,我想在变量中保存11,以便我以后可以使用它 如果用户输入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。 然后我尝试subtrace myVar - 1并尝试在MsgBox中打印,但它没有? 它显示我11 - 1而不是11减去1 我将使用Send,%myVar% - 1以后的MsgBox内容。

由于

2 个答案:

答案 0 :(得分:0)

我猜你需要按下按键的组合,如果按下1则然后按2来做你想要的东西12等等。

只使用全局变量

global myVar

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

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

1::
    Input Key, L1


    if Key=1
    {
        myVar:=11
        ; do code
    }
return

答案 1 :(得分:0)

autohotkey中有两种类型的赋值语句:=& :=

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

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

  3. 如果g_number = 5

    line1将评估为5 - 1,而line2将评估为4

    完整代码

    #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
    
相关问题