Autohotkey 遍历循环,但如果键与输入匹配,则只有 return 值

Autohotkey iterate through loop, but only return value if key matches input

假设我有以下

^!r::
  InputBox, input, Enter the string
  if (input = "trip")
  {
    TripFunction()
  }
  else if (input = "leave")
  {
    LeaveFunction()
  }
  else
  {
    Msgbox, That word isnt defined.
  }
Return

但是,预计必须添加很多不同的案例来测试,我认为最好的办法是将其放入一个数组中,然后遍历数组,寻找匹配的键,return获取值(函数),不再遍历字典。所以现在,我有这样的东西:

^!r::
  InputBox, input, Enter the string
  dict = { "trip": TripFunction(), "leave": LeaveFunction() }
  for k, v in dict
  {
    ...
    see if k = "trip", if so, return TripFunction(), if not, go to next
    item in array
    ...
  }
Return

我遇到的麻烦是,一旦它成功匹配字典中的一个键,它就会return所有关联的值。我应该把什么放在括号里才能达到我的目的?

您使用了错误的 equal sign operator(对非文字赋值使用 := 而不是 =

此外,在 dict = { "trip": TripFunction(), "leave": LeaveFunction() } 行中,执行了 tripfunction 和 leavefunction,这可能是您不想要的。

尝试:

^!r::
  InputBox, input, Enter the string
  dict := { "trip": "TripFunction", "leave": "LeaveFunction" }
  for k, v in dict
  {
    if(k==input) {
        %v%() ; documentation: https://autohotkey.com/docs/Functions.htm#DynCall
        break
    }
  }
Return

TripFunction() {
    msgbox trip
}

LeaveFunction() {
    msgbox leave
}