如何将AutoHotKey的代码设置为仅在chrome中运行?

时间:2018-05-04 17:08:00

标签: autohotkey

我想模拟一个"发现"双击Chrome中的单词时的操作。

我设法做到了这一点:

~LButton::
SetTitleMatchMode, 2  
#IfWinActive, ahk_class Chrome
If (A_TimeSincePriorHotkey<400) and (A_TimeSincePriorHotkey<>-1)
{
 SendInput ^c
 Sleep, 10
 SendInput ^f
 Sleep, 10
 SendInput ^v
}

Return

但它甚至可以运行非铬流程(双击一个单词时)

问题:

如何在Chrome中双击时才能运行此脚本?

2 个答案:

答案 0 :(得分:2)

#IfWinActive, ahk_exe Chrome.exe ;; start of IfWinActive condition, for me it didn't work with ahk_class so i changed it to ahk_exe
~LButton::
SetTitleMatchMode, 2  
If (A_TimeSincePriorHotkey<400) and (A_TimeSincePriorHotkey<>-1)
{
SendInput ^c
Sleep, 10
SendInput ^f
Sleep, 10
SendInput ^v
}
Return

~RButton::
SendInput {Escape}
Return
#IfWinActive ;; end of condition IfWinActive

答案 1 :(得分:1)

您可以使用WinGet获取窗口或活动窗口的标题 - 然后仅应用给定窗口处于活动状态的代码IF。

SetTitleMatchMode,2

IfWinActive, - Google Chrome

这是我发现的一个片段:

DetectHiddenText, On ; the url of the active Chrome window is hidden text...
SetTitleMatchMode, Slow ; ...we also need match mode 'slow' to detect it
; activate chrome window,
; just for demonstation:
WinActivate, ahk_class Chrome_WidgetWin_1
IfWinActive, ahk_class Chrome_WidgetWin_1 ; we only want to check for the hidden text if Chrome is the active window,
{
WinGetText, wintext, ahk_class Chrome_WidgetWin_1 ; if it is we grab the text from the window...
If InStr(wintext,"autohotkey.com")   ;       ...and if it contains a url that we want,
{
;### code conditional on url goes here ###
msgbox % "The active Chrome window is on the autohotkey.com domain! The page title and URL is:`n`n>" wintext                                 ; <<== we run the desired code here.
}
}
exitapp

但需要根据您的具体需求进行更改。