AHK:仅为一个特定的活动 window 分配热键,而不为其他人分配热键

AHK: Assign hotkey only for one specific active window and not for others

我刚刚完成了一段代码,它执行以下操作。当我在 Firefox 或 EndNote 中通过鼠标进行选择时,脚本会发送 Ctrl+c 并检查剪贴板中的正则表达式匹配项。如果匹配,它会更改剪贴板内容并显示工具提示。它适用于这两个程序。发送 Ctrl+c 时,Adobe Acrobat 有时会显示错误(即使用户按下 ctrl-c Acrobat 有时会显示著名的“复制到剪贴板时出错。发生内部错误”)。所以它决定分配一个F9 热键,但它适用于所有程序,而不仅仅是 Acrobat。我如何只为一个 window – Acrobat 分配热键?这是我的代码。我知道它很蹩脚 – 我是一般编程的新手,尤其是在 AHK。

#If WinActive("ahk_exe firefox.exe") || WinActive("ahk_exe EndNote.exe") || WinActive("ahk_exe Acrobat.exe")
        if WinActive("ahk_exe Acrobat.exe")
        F9::
        {
        Clipboard:=""
        send,^c
        ClipWait, 1
        ToolTip % Clipboard := RegExReplace(Clipboard, "\r\n", " ")
        SetTimer, ToolTipOff, -1000
        }
        return

    ~LButton::
        now := A_TickCount
        while GetKeyState("LButton", "P")
            continue
        if (A_TickCount-now > 500 )
        {   
            Send ^c
            if WinActive("ahk_exe firefox.exe")
            {
                If RegExMatch(Clipboard, "[0-9]\.\s[A-Za-z,]*\s[A-Za-z]*")
                {
                regex := "[0-9]\.\s*|\s?\([^)]*\)|\."
                replace := ""
                }
                else If RegExMatch(Clipboard,"[0-9]{2}[-\/][0-9]{2}[-\/][0-9]{4}")
                {
                Clipboard := RegExReplace(Clipboard, "^0", "")
                regex := "\/"
                replace := "."
                }
                else return
            }
            else if WinActive("ahk_exe EndNote.exe")
            {
                If RegExMatch(Clipboard, "[a-z]+\,\s[A-Z0-9‘“]")
                {
                regex := "\??!?\:|\?|!"
                replace := "."
                }
                else return
                }
            ToolTip % Clipboard := RegExReplace(Clipboard, regex, replace)
            SetTimer, ToolTipOff, -1000
        }
    return
#If

ToolTipOff:
    ToolTip
return

我在前几行中看到了一些非常基本的问题。让我解释一下...
AutoHotkey 中有两种类型的 if 语句 If#If。 你通常总是使用正常的 If-statements 除非你正在用热键做一些事情并且你希望特定的热键是上下文相关的。
以下是一些重要规则:
正常的 If 语句必须使用大括号 {} 来标记表达式为真时应执行的代码区域。如果您不使用大括号,If-语句的工作方式就好像您将大括号放在 If-语句正下方的第一个命令周围一样。
示例:

If WinActive("Firefox") {
    Send, Test
    MsgBox, The script just typed "Test.
}

另一个例子:

If WinActive("Firefox")
    MsgBox, Firefox is the active window.

普通 If 语句不能用于热键定义周围,只能在其中使用。

这是允许的:

F1::
    If (A_OSVersion = "WIN_7") {
        MsgBox, Your operating system is Windows 7 and you just pressed F1.
    }
Return

这不是:

If (A_OSVersion = "WIN_7") {
    F1::
        MsgBox, Your operating system is Windows 7 and you just pressed F1.
    Return
}

但有一种解决方法,那就是 #If-statements。
#If-语句从不使用花括号。
它们只能用于热键定义。
它们只能被另一个 #If 语句关闭。
(简单地使用一个空的 #If 来关闭它是很常见的。)

示例:

#If (A_OSVersion = "WIN_7")
    F1::
        MsgBox, Your operating system is Windows 7 and you just pressed F1.
    Return
#If

一个更复杂的例子:

#If (A_ScreenWidth >= 1920)
    F1::
        MsgBox, Your your screen is at least 1920 pixels wide.
    Return
    F2::
        MsgBox, Your operating system is %A_OSVersion%.
    Return
#If (A_ScreenWidth < 1920)
    F1::
        MsgBox, Your your screen width is smaller than 1920 pixels.
    Return
#If

正如您现在可能已经猜到的那样,热键定义总是以这样的模式开始 hotkey:: 并以 Return 结束。尽管您可以在一行中定义热键。 示例:

F1::MsgBox, Hello!
F2::a ;This will remap the F2 key to an a-key. 

热键本身从不使用大括号!尽管热键中的 If 语句仍然必须根据前面提到的规则使用它们。