AHK:仅为一个特定的活动窗口分配热键,而不为其他窗口分配热键 [英] AHK: Assign hotkey only for one specific active window and not for others

查看:35
本文介绍了AHK:仅为一个特定的活动窗口分配热键,而不为其他窗口分配热键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have just done a piece of code that does the following thing. When I make a selection by mouse in Firefox or EndNote, the script sents a Ctrl+c and checks the clipboard for a regex match. If there is a match, it changes the clipboard contents and shows a tooltip. It works fine for these two programs. Adobe Acrobat sometimes shows an error when a Ctrl+c is sent (even if a user presses a ctrl-c Acrobat sometimes shows famous "There was an error while copying to the Clipboard. An internal error occurred). So it decided to assign an F9 hotkey, but it works for all programs and not just for Acrobat. How do I assign an hotkey for only one window – Acrobat? Here's my code. I know it's lame – I am a newbie to programming in general, and in AHK in particular.

#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, "
", " ")
        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 语句,除非您使用热键执行某些操作并且您希望特定热键与上下文相关.
以下是一些重要规则:
正常的If-语句必须使用花括号{}来标记如果表达式为真则应该执行的代码区域.如果您不使用花括号,If 语句的作用就如同您在 If 语句下直接将第一个命令周围放置了花括号一样.
示例:

I see some very fundamental problems in the first few lines. Let me explain...
There are two types of if-statements in AutoHotkey If and #If. You usually always use the normal If-statements unless you are doing something with hotkeys and you want specific hotkeys to be context-sensitive.
Here are some important rules:
Normal If-statements have to use curly braces {} to mark the area of code that should be executed if the expression is true. If you don't use curly braces, the If-statement will work as if you had put curly braces around the first command directly under the If-statement.
Example:

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

另一个例子:

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

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

Normal If-statements cannot be used around a hotkey definition, but only within it.

这是允许的:

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-语句.
#If - 语句永远不要使用花括号.
它们只能用于热键定义.
它们只能被另一个 #If 语句关闭.
(简单地使用一个空的 #If 来关闭它是很常见的.)

But there is a way around that and that is #If-statements.
#If-statements don't use curly braces ever.
They can only be used on hotkey definitions.
And they can only be closed by another #If-statement.
(It's very common to simply use an empty #If to close it.)

示例:

#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 结束.尽管您可以在一行中定义热键.例子:

As you might have guessed by now, hotkey definitions are always started by a pattern like this hotkey:: and closed by a Return. Although you can define hotkeys on a single line. Examples:

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

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

Hotkeys by themselves do never use curly braces! Though an If-statement within a hotkey still has to use them according to the before mentioned rules.

这篇关于AHK:仅为一个特定的活动窗口分配热键,而不为其他窗口分配热键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆