如何在 Autohotkey 中重复发送密钥

How to send a key repeatedly in Autohotkey

我想编写一个 AutoHotkey 脚本,它循环一个键 X 次。

例如,这是一个脚本,它在文件资源管理器中用 F2 键的功能覆盖 ENTER 键的功能。

#IfWinActive ahk_class CabinetWClass
Enter::
Send, {F2}

#IfWinActive ahk_class CabinetWClass
Enter::
Send, {ENTER}

#IfWinActive

目标是按ENTER重命名一个select文件,然后按ENTER确认重命名。在刚刚重命名的同一个文件上按 ENTER 应该再次发送 F2 键(以防出现拼写错误)。

目前第二个块不起作用,因为我正在发送相同的密钥,如何解决这个问题?

您正在尝试重新绑定回车键两次。 重新绑定一个键就像说 "When I press this key, do this:" - 在这种情况下它在 #IfWinActive 下所以它更像是 "When this window is open and I press this key..."

当你分解它时你有 "When I press enter - press F2" 以及 "When I press enter, press enter"
您想要实现的是使重新绑定有条件 - 即它仅在特定条件下发送 F2。

如果没有更多上下文,很难知道如何提供帮助。有什么理由不能使用不同的组合键吗?喜欢 Ctrl + Shift + Enter?

类似于:

+^Enter::send, {F2}

基本上,您似乎在尝试将不同的任务分配给同一个热键,并且由于这是单独完成的,ahk 正在选择其中一项任务,运行 该任务且仅选择该任务。如果可以在热键中使用循环,那么我建议使用它在两个预期结果之间轮换。请看下面的例子:

temp:= 1

enter::
    if(temp==1)
    {
        Send, {ENTER}
        temp:=2
    }
    else if(temp==2)
    {
        Send, {F2}
        temp:=1
    }
return

1::
    Temp:=1
return

2::
    temp:=2
return

^x::ExitApp

我还添加了 1/2 的热键,让您可以手动决定结果,而不是在出现任何问题时专门指定。 哦,还有 ctrl+x 关闭宏。

在这种情况下,KeyWait 命令是你的朋友。

您处理第二个问题的方式仍有改进空间输入

#IfWinActive ahk_class CabinetWClass
   $Enter::
     sleep,100 ; giving time to detect the first Enter
     Send, {F2}
     Keywait, Enter, T5 D ; wait 5 seconds for the Enter to be pressed down
     If (ErrorLevel == 0)
     {
       Send, {Enter}
       sleep 200
       Send, {F2}
     }
     else
     {
       traytip, , timeout   ; Enter was not pressed down in 5 seconds
     }

   return