1

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.

  • Looking into this quickly, it seems variables are of local scope, so they die when the script ends. This one might help, through: special variable A_PriorKey (or A_PriorHotKey or A_ThisHotKey, located nearby in documentation). – TOOGAM Nov 21 '16 at 4:06
1

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.

  • Hi user3419297 thanks for your support, It worked like a charm even with the F1 key, I just wonder lets say I have lots of scripts that run only in MSword, (like 1pressed twice, 2pressed twice, 3 pressed twice, and other different like Ctrl Alt P) I just put #If WinActive("ahk_exe WINWORD.EXE") ; (1) at the very beginning of all of them, and only put #If ; turn off context sensitivity at the end of all of them, right? – litu16 Nov 21 '16 at 12:02
  • Answer to your first question: You only need one script for defining hotkeys or hotstrings in MSword and in any other program. I edited my answer to help you better understand, how context-sensitive hotkeys work.Please, delete your second question and add it after this comment in case my answer doesn't satisfy you. – user3419297 Nov 21 '16 at 13:14
  • Thanks user3419297, I got it, but what if in MSword I have script that is triggered when ` 1 (the key above tab and 1) is pressed in sequence, and another when just the 1 is pressed, both scripts only run in MSword, I mean, if I press the first key sequence will it trigger the second script also??? do they interfere with each other? Thanks Advanced. – litu16 Nov 21 '16 at 14:13
  • btw I have also some little question superuser.com/questions/1148186/… Thanks agian – litu16 Nov 21 '16 at 14:54
  • How do you define this sequence in the script? Add the code in your question or (better) make a new question in Super User. – user3419297 Nov 21 '16 at 14:58

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.