【问题标题】:AHK: Assign hotkey only for one specific active window and not for othersAHK:只为一个特定的活动窗口分配热键,而不为其他窗口分配热键
【发布时间】:2016-07-09 13:15:01
【问题描述】:

我刚刚完成了一段代码来完成以下事情。当我在 Firefox 或 EndNote 中通过鼠标进行选择时,脚本会发送 Ctrl+c 并检查剪贴板是否匹配正则表达式。如果匹配,它会更改剪贴板内容并显示工具提示。它适用于这两个程序。 Adobe Acrobat 有时会在发送 Ctrl+c 时显示错误(即使用户按下 ctrl-c Acrobat 有时也会显示著名的“复制到剪贴板时出错。发生内部错误)。因此它决定分配一个F9 热键,但它适用于所有程序,而不仅仅是 Acrobat。如何为一个窗口分配热键 - 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


    【解决方案1】:

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

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

    另一个例子:

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

    普通的If-statements 不能在热键定义周围使用,而只能在其中使用。

    这是允许的:

    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-statements 永远不要使用花括号。
    它们只能用于热键定义。
    而且它们只能被另一个#If-statement 关闭。
    (简单地使用一个空的#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-statement 仍然必须根据前面提到的规则使用它们。

    【讨论】:

    • 谢谢。我现在无法访问 AHK,但我认为您的回答会对我有很大帮助。如果代码有问题,我会回复。
    猜你喜欢
    • 2011-02-19
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    相关资源
    最近更新 更多