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

How can I set AutoHotKey's code to run only in chrome?

我想在Chrome中双击一个单词时模拟"find"操作。

我已经做到了:

~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 进程(当双击一个词时)也是如此

问题:

只有在 chrome 中双击时,我怎样才能制作此脚本 运行?

您可以使用 WinGet 获取 window 或活动 window 的标题 - 然后仅在给定 window 活动时应用代码。

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

但需要对其进行更改以满足您的特定需求。

#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