![Autohotkey - 热键 1 执行某项操作,键序列 11 执行另一项操作](https://linux22.com/image/1513428/Autohotkey%20-%20%E7%83%AD%E9%94%AE%201%20%E6%89%A7%E8%A1%8C%E6%9F%90%E9%A1%B9%E6%93%8D%E4%BD%9C%EF%BC%8C%E9%94%AE%E5%BA%8F%E5%88%97%2011%20%E6%89%A7%E8%A1%8C%E5%8F%A6%E4%B8%80%E9%A1%B9%E6%93%8D%E4%BD%9C.png)
我对 AHK 还不太熟悉,我希望 Autohotkey 能够在我按下 MS Word 中的热键“1”时输入 HELLOW,但同时在同一个应用程序 (MS Word) 中,我希望组合键“11”(快速按下两次键 1)输入 BYE,这可能吗?当我输入“1”时,AHK 会输入“1HELLOW”吗?当我输入“11”时,AHK 会输入“11BYE”吗?是否可以用 F1 来执行相同的脚本?我的意思是 F1,以及按键序列 F1F1(快速按下两次 F1)
到目前为止我已经尝试过
~1::
;400 is the maximum allowed delay (in milliseconds) between presses.
if (A_PriorHotKey = "~1" AND A_TimeSincePriorHotkey < 400)
{
Msgbox,Double press detected.
}
else if (A_PriorHotKey = "~1" AND A_TimeSincePriorHotkey > 400)
{
Msgbox,Single press detected.
}
Sleep 0
KeyWait 1
return
但是只有在我第一次按下键序列 11(快速按两次 1)时它才有效,然后它总是只识别 1 键,为什么???
~1::
if (A_PriorHotkey <> "~1" or A_TimeSincePriorHotkey > 400)
{
; Too much time between presses, so this isn't a double-press.
KeyWait, 1
return
}
MsgBox You double-pressed the 1 key.
return
这也无助于获取两个热键 (1 和 11)。
谢谢高级。
答案1
使用 SetTimer 效果最佳:
; The following hotkeys work only if MS-WORD is the active window:
#If WinActive("ahk_exe WINWORD.EXE") ; (1)
1::
if 1_presses > 0
{
1_presses += 1
SetTimer Key1, 300
return
}
1_presses = 1
SetTimer Key1, 300
return
Key1:
SetTimer Key1, off
if 1_presses = 2
SendInput, BYE
else
SendInput, HELLOW
1_presses = 0
return
F2:: MsgBox, You pressed F2 in MS-Word
; The following hotkeys work only if NOTEPAD is the active window:
#If WinActive("ahk_exe NOTEPAD.EXE")
1:: Send 2
F2:: MsgBox, You pressed F2 in NOTEPAD
#If ; turn off context sensitivity (= end of context-sensitive hotkeys)
; The following hotkeys work only if MS-WORD or NOTEPAD is NOT the active window (because they are already defined in those programs):
1:: Send 3
F2:: MsgBox, You pressed F2 while MS-WORD or NOTEPAD is NOT the active window
; The following hotkeys work in all windows (incl. MS-WORD and NOTEPAD because they are NOT defined in those programs)
F3:: MsgBox, You pressed F3
Esc:: ExitApp
https://autohotkey.com/docs/commands/SetTimer.htm#Examples(示例 3)
(1)就像#如果赢指令,#如果创建上下文相关的热键和热字符串并且是位置相关的:它会影响脚本中物理上位于其下方的所有热键和热字符串。