ahk 热键打开窗口,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, "\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

解决方案

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.

}

Another example:

If WinActive("Firefox")

MsgBox, Firefox is the active window.

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

This is allowed:

F1::

If (A_OSVersion = "WIN_7") {

MsgBox, Your operating system is Windows 7 and you just pressed F1.

}

Return

This is NOT:

If (A_OSVersion = "WIN_7") {

F1::

MsgBox, Your operating system is Windows 7 and you just pressed F1.

Return

}

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.)

Examples:

#If (A_OSVersion = "WIN_7")

F1::

MsgBox, Your operating system is Windows 7 and you just pressed F1.

Return

#If

A more complex example:

#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

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.

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值