Skip to main content
 首页 » 编程设计

autohotkey之如何在 AutoHotKey 中实现类似 select-case-esac 结构的东西

2026年05月17日82txw1958

我不确定如何开始这个。我查看了 AHK 的帮助文件,但没有结果。

我需要建立这样的构造(这既不在 UNIX shell 中也不在 AHK 脚本语言中。只是展示我所追求的想法)

while true 
do 
 
gosub screenscrape 
; here, all text on page is on clipboard 
; variable "input" is a predefined portion of the clipboard 
 
case $input in 
string-1) 
gosub subroutione1;; 
string-2) 
gosub subroutine2;; 
 
... 
 
*) 
echo "not found" 
sleep 1minute 
 
done 

为了让事情变得更复杂,标记为string-1string-n 的部分也是变量,根据时间从几个不同的文件中读入当天或由事件触发。

有人可以为此指出正确的方向吗?

请您参考如下方法:

Autohotkey 没有任何 case 语句。每个人所做的就是使用ifelse 语句。 (我知道那种感觉。我也喜欢case)

给你的一些提示:

  • 在 autohotkey 的变量名中不能有 $-
  • ...但是您可以使用下划线。
  • 你不需要用 ; 来结束你的行。

这是为您翻译的 autohotkey。

gosub screenscrape 
; here, all text on page is on clipboard 
; variable "input" is a predefined portion of the clipboard 
 
if(string1){   ;if the variable is not blank it will evaluate to `true` 
    gosub subroutine1 
}else if(string2){ 
    gosub subroutine2 
}else{ 
    msgbox not found 
    sleep 60000  ;milliseconds 
} 

另外,您可以在 autohotkey 中使用真正的功能——您不必依赖 gosubs。如果您使用 gosub,请确保在它们的末尾放置一个 return。您还应该阅读有关“自动执行”部分的自动热键文档。