Monday, 26 August 2019

keyboard - Autohotkey - hotkey 1 do something, key sequence 11 do another thing


Im pretty new in AHK, I would like Autohotkey to whenever I press the hotkey "1" in MS Word type HELLOW, but at the same time in the same app (MS Word) I want the key combination "11" (key 1 pressed two times very quickly) to type BYE, is this possible? will AHK type "1HELLOW" when I type "1", will it type "11BYE" when I type "11"? is it possible to do the same script but with F1 instead? I mean F1, and the key sequence F1F1 (F1 pressed twice very quickly)


So far I have tried this


~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

But it just work the first time I press the key sequence 11 (1 pressed twice quickly) then it will always recognize only the 1 key, why???


~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

this doesn't help to get the two hotkeys, (1 and 11) either.


Thanks Advanced.



Answer



It works best by using 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 (Example #3)


(1) Like the #IfWin directives, #If creates context-sensitive hotkeys and hotstrings and is positional: it affects all hotkeys and hotstrings physically beneath it in the script.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...